| 1 |
/* |
| 2 |
* Copyright (C)2005-2022 Haxe Foundation |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 5 |
* copy of this software and associated documentation files (the "Software"), |
| 6 |
* to deal in the Software without restriction, including without limitation |
| 7 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 9 |
* Software is furnished to do so, subject to the following conditions: |
| 10 |
* |
| 11 |
* The above copyright notice and this permission notice shall be included in |
| 12 |
* all copies or substantial portions of the Software. |
| 13 |
* |
| 14 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 |
* DEALINGS IN THE SOFTWARE. |
| 21 |
*/ |
| 22 |
|
| 23 |
open Nekoml.Ast |
| 24 |
|
| 25 |
type error_msg { |
| 26 |
Invalid_character : char; |
| 27 |
Unterminated_string; |
| 28 |
Unclosed_comment; |
| 29 |
Invalid_escaped_character : int; |
| 30 |
Invalid_escape; |
| 31 |
} |
| 32 |
|
| 33 |
exception Error : (error_msg , pos) |
| 34 |
|
| 35 |
function error_msg(msg) { |
| 36 |
match msg { |
| 37 |
| Invalid_character c -> sprintf "Invalid character '%s'" (String.escape_char c) |
| 38 |
| Unterminated_string -> "Unterminated string" |
| 39 |
| Unclosed_comment -> "Unclosed comment" |
| 40 |
| Invalid_escaped_character n -> sprintf "Invalid escaped character %d" n |
| 41 |
| Invalid_escape -> "Invalid escape sequence" |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
function error(l,msg) { |
| 46 |
throw Error(msg,Lexer.curpos l) |
| 47 |
} |
| 48 |
|
| 49 |
var keywords = { |
| 50 |
var h = Hashtbl.create(); |
| 51 |
List.iter (function(k) { Hashtbl.add h (s_keyword k) k }) |
| 52 |
[Var;If;Else;Function;Try;Catch;Type;Match;Then;When;While;Exception]; |
| 53 |
h |
| 54 |
} |
| 55 |
|
| 56 |
function mk(l,t) { |
| 57 |
(t,Lexer.curpos l) |
| 58 |
} |
| 59 |
|
| 60 |
function mk_ident(l) { |
| 61 |
var s = Lexer.current l; |
| 62 |
mk l (try Keyword (Hashtbl.find keywords s) catch { Not_found -> Const (Ident s) }) |
| 63 |
} |
| 64 |
|
| 65 |
function mk_int(l) { |
| 66 |
mk l (Const (Int (int (Lexer.current l)))) |
| 67 |
} |
| 68 |
|
| 69 |
function mk_float(l) { |
| 70 |
mk l (Const (Float (Lexer.current l))) |
| 71 |
} |
| 72 |
|
| 73 |
function mk_binop(l) { |
| 74 |
mk l (Binop (Lexer.current l)); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
var ident = "[a-z_][a-zA-Z0-9_]*"; |
| 79 |
var modident = "[A-Z][a-zA-Z0-9_]*"; |
| 80 |
var binop = "[-!=*/<>&|%+:]"; |
| 81 |
var number = "[0-9]"; |
| 82 |
var numbers = number + "+"; |
| 83 |
var spaces = "[ \r\n\t]+"; |
| 84 |
|
| 85 |
var token = &Lexer.empty(); |
| 86 |
|
| 87 |
function comment(l) { |
| 88 |
Lexer.token l (*Neko.Lexer.ecomment) |
| 89 |
} |
| 90 |
|
| 91 |
function str(l) { |
| 92 |
Lexer.token l (*Neko.Lexer.estring) |
| 93 |
} |
| 94 |
|
| 95 |
token := Lexer.build [ |
| 96 |
(";", function(l) { mk l Semicolon }); |
| 97 |
(".", function(l) { mk l Dot }); |
| 98 |
(",", function(l) { mk l Comma }); |
| 99 |
("{", function(l) { mk l BraceOpen }); |
| 100 |
("}", function(l) { mk l BraceClose }); |
| 101 |
(spaces + "(", function(l) { mk l (ParentOpen true) }); |
| 102 |
("(", function(l) { mk l (ParentOpen false) }); |
| 103 |
(")", function(l) { mk l ParentClose }); |
| 104 |
("\\[", function(l) { mk l BracketOpen }); |
| 105 |
("]", function(l) { mk l BracketClose }); |
| 106 |
("'", function(l) { mk l Quote }); |
| 107 |
("|", function(l) { mk l Vertical }); |
| 108 |
(spaces, function(l) { Lexer.token l (*token) }); |
| 109 |
( "0x[0-9a-fA-F]+", mk_int ); |
| 110 |
( numbers, mk_int ); |
| 111 |
( numbers + "." + number + "*", mk_float); |
| 112 |
( "." + numbers, mk_float ); |
| 113 |
("true", function(l) { mk l (Const (Bool true)) }); |
| 114 |
("false", function(l) { mk l (Const (Bool false))}); |
| 115 |
("\"", function(l) { |
| 116 |
var p1 = Lexer.curpos l; |
| 117 |
var buf = Lexer.data l; |
| 118 |
Buffer.reset buf; |
| 119 |
try str l catch { Exit -> throw Error(Unterminated_string,p1) }; |
| 120 |
var p2 = Lexer.curpos l; |
| 121 |
(Const String(Buffer.string buf) , Lexer.punion p1 p2) |
| 122 |
}); |
| 123 |
("/\\*", function(l) { |
| 124 |
var p1 = Lexer.curpos l; |
| 125 |
var buf = Lexer.data l; |
| 126 |
Buffer.reset buf; |
| 127 |
try comment l catch { Exit -> throw Error(Unclosed_comment,p1) }; |
| 128 |
var p2 = Lexer.curpos l; |
| 129 |
(Comment(Buffer.string buf) , Lexer.punion p1 p2) |
| 130 |
}); |
| 131 |
("'\\\\n'", function(l) { mk l Const(Char '\n') }); |
| 132 |
("'\\\\t'", function(l) { mk l Const(Char '\t') }); |
| 133 |
("'\\\\r'", function(l) { mk l Const(Char '\r') }); |
| 134 |
("'\\\\''", function(l) { mk l Const(Char '\'') }); |
| 135 |
("'\\\\\\\\'", function(l) { mk l Const(Char '\\') }); |
| 136 |
("'\\\\\"'", function(l) { mk l Const(Char '"') }); |
| 137 |
("'[^\\\\]'", function(l) { mk l Const(Char (String.get Lexer.current(l) 1)) }); |
| 138 |
("'\\\\[0-9][0-9][0-9]'", function(l) { |
| 139 |
var s = String.sub (Lexer.current l) 2 3; |
| 140 |
var n = int s; |
| 141 |
if n > 255 then error l Invalid_escaped_character(n); |
| 142 |
mk l (Const (Char (chr n))) |
| 143 |
}); |
| 144 |
("//[^\r\n]*\n?", function(l) { |
| 145 |
var s = Lexer.current l; |
| 146 |
var len = String.length s; |
| 147 |
var n = (if String.get s (len - 1) == '\r' then 3 else 2); |
| 148 |
mk l CommentLine(String.sub s 0 (len - n)) |
| 149 |
}); |
| 150 |
("\\[<", function(l) { mk l StreamOpen }); |
| 151 |
(">]", function(l) { mk l StreamClose }); |
| 152 |
("->", function(l) { mk l Arrow }); |
| 153 |
(binop + binop + "?", mk_binop); |
| 154 |
(">>>" , mk_binop); |
| 155 |
("===" , mk_binop); |
| 156 |
("!==" , mk_binop); |
| 157 |
("or" , mk_binop); |
| 158 |
("and" , mk_binop); |
| 159 |
("xor" , mk_binop); |
| 160 |
(ident , mk_ident); |
| 161 |
(modident , function(l) { mk l Const(Constr (Lexer.current l)) }); |
| 162 |
] (function(l) { |
| 163 |
match Lexer.char l { |
| 164 |
| None -> mk l Eof |
| 165 |
| Some c -> error l (Invalid_character c) |
| 166 |
} |
| 167 |
}); |
| 168 |
|