summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/curly_lexer.mll40
1 files changed, 32 insertions, 8 deletions
diff --git a/src/curly_lexer.mll b/src/curly_lexer.mll
index 6254358..6d2bc87 100644
--- a/src/curly_lexer.mll
+++ b/src/curly_lexer.mll
@@ -7,8 +7,18 @@ exception Error of string
}
rule token = parse
-| [' ' '\t' '\n']
+| [' ' '\t' '\r']
{ token lexbuf }
+| '\n'
+ { Lexing.new_line lexbuf; token lexbuf }
+| '"'
+ { read_string (Buffer.create 16) lexbuf }
+| '''
+ { read_single_quoted_string (Buffer.create 16) lexbuf }
+| "//" [^ '\n']+ '\n'
+ { Lexing.new_line lexbuf ; token lexbuf }
+| "/*"
+ { read_comment (Buffer.create 16) lexbuf }
| '{'
{ LEFT_BRACE }
| '}'
@@ -19,16 +29,10 @@ rule token = parse
{ RIGHT_BRACKET }
| ';'
{ SEMI }
-| ['a' -'z' 'A' - 'Z' '0' - '9' '.' '/' ':' '@' '$' '-' ]+ as s
+| [^ ' ' '\t' '\n' '\r' '{' '}' '[' ']' ';' '#' '"' ''' ]+ 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))) }
@@ -52,6 +56,26 @@ and read_string buf =
| _ { raise (Error (Printf.sprintf "Illegal string character: %s" (Lexing.lexeme lexbuf))) }
| eof { raise (Error ("String is not terminated")) }
+and read_single_quoted_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_single_quoted_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
| "*/"