diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/curly_lexer.mll | 62 | ||||
-rw-r--r-- | src/curly_parser.mly | 52 |
2 files changed, 114 insertions, 0 deletions
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 <string> IDENTIFIER +%token <string> STRING +%token <string> COMMENT +%token LEFT_BRACE +%token RIGHT_BRACE +%token LEFT_BRACKET +%token RIGHT_BRACKET +%token SEMI +%token EOF + +%start <Config_tree.t> 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 + } +; |