From 321d1548d46138afdb458c4646eb32ef8683d05b Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Thu, 5 Jan 2017 00:55:22 +0700 Subject: Initial implementation of the curly config parser. --- _oasis | 10 ++++++ _tags | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++- src/curly_lexer.mll | 62 +++++++++++++++++++++++++++++++++++ src/curly_parser.mly | 52 +++++++++++++++++++++++++++++ 4 files changed, 216 insertions(+), 1 deletion(-) create mode 100644 src/curly_lexer.mll create mode 100644 src/curly_parser.mly diff --git a/_oasis b/_oasis index f0f45d2..0d3d539 100644 --- a/_oasis +++ b/_oasis @@ -38,6 +38,16 @@ Library "value_checker" FindlibParent: vyconf BuildDepends: pcre +Library "curly_lexer" + Path: src + Modules: Curly_lexer + FindlibParent: vyconf + +Library "curly_parser" + Path: src + Modules: Curly_parser + FindlibParent: vyconf + Library "util" Path: src Modules: Util diff --git a/_tags b/_tags index 9dc6d49..c8cb0b2 100644 --- a/_tags +++ b/_tags @@ -1,8 +1,9 @@ # OASIS_START -# DO NOT EDIT (digest: e1fbaa814f6e84974fc385e5d815bf45) +# DO NOT EDIT (digest: 11f71f9fb0db5e0d6e22aa91f3d802d1) # Ignore VCS directories, you can use the same kind of rule outside # OASIS_START/STOP if you want to exclude directories that contains # useless stuff for the build process +true: annot, bin_annot <**/.svn>: -traverse <**/.svn>: not_hygienic ".bzr": -traverse @@ -13,8 +14,98 @@ ".git": not_hygienic "_darcs": -traverse "_darcs": not_hygienic +# Library vyconf +"src/vyconf.cmxs": use_vyconf +# Library vytree +"src/vytree.cmxs": use_vytree +# Library reference_tree +"src/reference_tree.cmxs": use_reference_tree +# Library config_tree +"src/config_tree.cmxs": use_config_tree +# Library value_checker +"src/value_checker.cmxs": use_value_checker +: pkg_pcre +# Library curly_lexer +"src/curly_lexer.cmxs": use_curly_lexer +# Library curly_parser +"src/curly_parser.cmxs": use_curly_parser +# Library util +"src/util.cmxs": use_util +: pkg_xml-light +# Library session +"src/session.cmxs": use_session +# Library vyconf_config +"src/vyconf_config.cmxs": use_vyconf_config +: pkg_ppx_deriving.show +# Library directories +"src/directories.cmxs": use_directories +# Library message +"src/message.cmxs": use_message +: pkg_ppx_deriving_yojson +# Library startup +"src/startup.cmxs": use_startup +: pkg_lwt.log # Executable vyconfd +: pkg_fileutils +: pkg_lwt +: pkg_lwt.ppx +: pkg_lwt.unix +: pkg_ppx_deriving.runtime +: pkg_ppx_deriving_yojson.runtime +: pkg_toml +: use_vyconf +: pkg_fileutils +: pkg_lwt +: pkg_lwt.ppx +: pkg_lwt.unix +: pkg_ppx_deriving.runtime +: pkg_ppx_deriving_yojson.runtime +: pkg_toml +: use_vyconf +# Executable vytree_test +: pkg_oUnit +: use_vyconf +# Executable reference_tree_test +: pkg_oUnit +: pkg_pcre +: pkg_xml-light +: use_vyconf +# Executable config_tree_test +: pkg_oUnit +: use_vyconf +# Executable vylist_test +: pkg_oUnit +: use_vyconf +# Executable value_checker_test +: pkg_oUnit +: pkg_pcre +: use_vyconf +# Executable util_test +: pkg_oUnit +: pkg_xml-light +: use_vyconf +# Executable vyconf_config_test +: pkg_oUnit +: pkg_ppx_deriving.runtime +: pkg_toml +: use_vyconf +: pkg_ppx_deriving.runtime +: pkg_toml +# Executable session_test +: pkg_fileutils +: pkg_oUnit +: pkg_pcre +: pkg_xml-light +: use_vyconf +: pkg_fileutils +: pkg_oUnit +: pkg_pcre +: pkg_xml-light +# Executable vytree_load_test +: use_vyconf +: use_vyconf # OASIS_STOP true: strict_sequence true: bin_annot +true: use_menhir, explain diff --git a/src/curly_lexer.mll b/src/curly_lexer.mll new file mode 100644 index 0000000..8086648 --- /dev/null +++ b/src/curly_lexer.mll @@ -0,0 +1,62 @@ +{ + +open Curly_parser + +exception Error of string + +} + +rule token = parse +| [' ' '\t' '\n'] + { token lexbuf } +| '{' + { LEFT_BRACE } +| '}' + { RIGHT_BRACE } +| '[' + { LEFT_BRACKET } +| ']' + { RIGHT_BRACKET } +| ';' + { SEMI } +| ['a' - 'z' 'A' - 'Z' '0' - '9' '-' '.' ]+ as s + { IDENTIFIER s} +| eof + { EOF } +| '"' + { read_string (Buffer.create 16) lexbuf } +| "//" [^ '\n']+ '\n' + { Lexing.new_line lexbuf ; token lexbuf } +| "/*" + { read_comment (Buffer.create 16) lexbuf } +| _ +{ raise (Error (Printf.sprintf "At offset %d: unexpected character.\n" (Lexing.lexeme_start lexbuf))) } + +and read_string buf = + parse + | '"' { STRING (Buffer.contents buf) } + | '\\' '/' { Buffer.add_char buf '/'; read_string buf lexbuf } + | '\\' '\\' { Buffer.add_char buf '\\'; read_string buf lexbuf } + | '\\' 'b' { Buffer.add_char buf '\b'; read_string buf lexbuf } + | '\\' 'f' { Buffer.add_char buf '\012'; read_string buf lexbuf } + | '\\' 'n' { Buffer.add_char buf '\n'; read_string buf lexbuf } + | '\\' 'r' { Buffer.add_char buf '\r'; read_string buf lexbuf } + | '\\' 't' { Buffer.add_char buf '\t'; read_string buf lexbuf } + | '\\' '\'' { Buffer.add_char buf '\''; read_string buf lexbuf } + | '\\' '"' { Buffer.add_char buf '"'; read_string buf lexbuf } + | '\n' { Lexing.new_line lexbuf; Buffer.add_char buf '\n'; read_string buf lexbuf } + | [^ '"' '\\']+ + { Buffer.add_string buf (Lexing.lexeme lexbuf); + read_string buf lexbuf + } + | _ { raise (Error (Printf.sprintf "Illegal string character: %s" (Lexing.lexeme lexbuf))) } + | eof { raise (Error ("String is not terminated")) } + +and read_comment buf = + parse + | "*/" + { COMMENT (Buffer.contents buf) } + | _ + { Buffer.add_string buf (Lexing.lexeme lexbuf); + read_comment buf lexbuf + } diff --git a/src/curly_parser.mly b/src/curly_parser.mly new file mode 100644 index 0000000..0053b56 --- /dev/null +++ b/src/curly_parser.mly @@ -0,0 +1,52 @@ +%{ + open Config_tree +%} + +%token IDENTIFIER +%token STRING +%token COMMENT +%token LEFT_BRACE +%token RIGHT_BRACE +%token LEFT_BRACKET +%token RIGHT_BRACKET +%token SEMI +%token EOF + +%start config +%% + +opt_comment: + | (* empty *) { None } + | c = COMMENT { Some c } + +value: + | v = STRING + { v } + | v = IDENTIFIER + { v } +; + +values: + | v = value { [v] } + | LEFT_BRACKET; vs = separated_nonempty_list(SEMI, value); RIGHT_BRACKET + { vs } +; + +leaf_node: + | comment = opt_comment; name = IDENTIFIER; values = values; SEMI + { Vytree.make_full {default_data with values=values; comment=comment} name []} +; + +node: + | comment = opt_comment; name = IDENTIFIER; LEFT_BRACE; children = list(node_content); RIGHT_BRACE + { let node = Vytree.make_full {default_data with comment=comment} name [] in List.fold_left Vytree.adopt node children } +; + +node_content: n = node { n } | n = leaf_node { n }; + +%public config: + ns = list(node); EOF + { + let root = make "root" in List.fold_left Vytree.adopt root ns + } +; -- cgit v1.2.3