| 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 |
type pos = Lexer.pos |
| 24 |
|
| 25 |
type constant { |
| 26 |
Int : int; |
| 27 |
Char : char; |
| 28 |
Bool : bool; |
| 29 |
Float : string; |
| 30 |
String : string; |
| 31 |
Ident : string; |
| 32 |
Constr : string; |
| 33 |
Module : (string list , constant); |
| 34 |
} |
| 35 |
|
| 36 |
type keyword { |
| 37 |
Var; |
| 38 |
If; |
| 39 |
Else; |
| 40 |
Function; |
| 41 |
Try; |
| 42 |
Catch; |
| 43 |
Type; |
| 44 |
Match; |
| 45 |
Then; |
| 46 |
When; |
| 47 |
While; |
| 48 |
Exception; |
| 49 |
} |
| 50 |
|
| 51 |
type token { |
| 52 |
Eof; |
| 53 |
Semicolon; |
| 54 |
Dot; |
| 55 |
Comma; |
| 56 |
Quote; |
| 57 |
BraceOpen; |
| 58 |
BraceClose; |
| 59 |
ParentOpen : bool; |
| 60 |
ParentClose; |
| 61 |
BracketOpen; |
| 62 |
BracketClose; |
| 63 |
Arrow; |
| 64 |
Vertical; |
| 65 |
StreamOpen; |
| 66 |
StreamClose; |
| 67 |
Const : constant; |
| 68 |
Keyword : keyword; |
| 69 |
Binop : string; |
| 70 |
Comment : string; |
| 71 |
CommentLine : string; |
| 72 |
} |
| 73 |
|
| 74 |
type type_path { |
| 75 |
EType : (type_path option , string list , string); |
| 76 |
EPoly : string; |
| 77 |
ETuple : type_path list; |
| 78 |
EArrow : (type_path , type_path); |
| 79 |
} |
| 80 |
|
| 81 |
type type_decl { |
| 82 |
EAbstract; |
| 83 |
EAlias : type_path; |
| 84 |
ERecord : (string , bool , type_path) list; |
| 85 |
EUnion : (string , type_path option) list; |
| 86 |
} |
| 87 |
|
| 88 |
type arg { |
| 89 |
ATyped : (arg , type_path); |
| 90 |
ANamed : string; |
| 91 |
ATuple : arg list; |
| 92 |
} |
| 93 |
|
| 94 |
type pattern; |
| 95 |
type expr; |
| 96 |
type stream_item; |
| 97 |
|
| 98 |
type pattern_decl { |
| 99 |
PIdent : string; |
| 100 |
PConst : constant; |
| 101 |
PTuple : pattern list; |
| 102 |
PRecord : ((string , pattern) list, int); |
| 103 |
PConstr : (string list , string , pattern option); |
| 104 |
PAlias : (string , pattern); |
| 105 |
PTyped : (pattern , type_path); |
| 106 |
PStream : (stream_item list , int); |
| 107 |
} |
| 108 |
|
| 109 |
type stream_item { |
| 110 |
SPattern : pattern; |
| 111 |
SExpr : (string list , expr); |
| 112 |
SMagicExpr : (pattern , int); |
| 113 |
} |
| 114 |
|
| 115 |
type expr_decl { |
| 116 |
EConst : constant; |
| 117 |
EBlock : expr list; |
| 118 |
EField : (expr , string); |
| 119 |
ECall : (expr , expr list); |
| 120 |
EArray : (expr , expr); |
| 121 |
EVar : ((string , type_path option) list , expr); |
| 122 |
EIf : (expr , expr , expr option); |
| 123 |
EFunction : (bool , string option , arg list , expr , type_path option); |
| 124 |
EBinop : (string , expr , expr); |
| 125 |
EUnop : (string , expr); |
| 126 |
ETypeAnnot : (expr , type_path); |
| 127 |
ETupleDecl : expr list; |
| 128 |
ETypeDecl : (string list , string , type_decl); |
| 129 |
EErrorDecl : (string , type_path option); |
| 130 |
ERecordDecl : (string , expr) list; |
| 131 |
EMatch : (expr , (pattern list , expr option , expr) list); |
| 132 |
ETry : (expr , (pattern list , expr option , expr) list); |
| 133 |
ETupleGet : (expr , int); |
| 134 |
EApply : (expr , expr list); |
| 135 |
EWhile : (expr , expr); |
| 136 |
} |
| 137 |
|
| 138 |
type pattern = (pattern_decl , pos) |
| 139 |
type expr = (expr_decl , pos) |
| 140 |
|
| 141 |
var pos = snd; |
| 142 |
|
| 143 |
function rec s_constant(c) { |
| 144 |
match c { |
| 145 |
| Int i -> string i |
| 146 |
| Float s -> s |
| 147 |
| Bool b -> if b then "true" else "false" |
| 148 |
| Char c -> "\"" + String.escape_char c + "\"" |
| 149 |
| String s -> "\"" + String.escape s + "\"" |
| 150 |
| Ident s -> s |
| 151 |
| Constr s -> s |
| 152 |
| Module (l,c) -> String.concat "." l + "." + s_constant c |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
function s_path(path,n) { |
| 157 |
match path { |
| 158 |
| [] -> n |
| 159 |
| _ -> String.concat "." path + "." + n |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
function s_keyword(k) { |
| 164 |
match k { |
| 165 |
| Var -> "var" |
| 166 |
| If -> "if" |
| 167 |
| Else -> "else" |
| 168 |
| Function -> "function" |
| 169 |
| Try -> "try" |
| 170 |
| Catch -> "catch" |
| 171 |
| Type -> "type" |
| 172 |
| Match -> "match" |
| 173 |
| Then -> "then" |
| 174 |
| When -> "when" |
| 175 |
| While -> "while" |
| 176 |
| Exception -> "exception" |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
function s_token(t) { |
| 181 |
match t { |
| 182 |
| Eof -> "<eof>" |
| 183 |
| Semicolon -> ";" |
| 184 |
| Dot -> "." |
| 185 |
| Comma -> "," |
| 186 |
| Quote -> "'" |
| 187 |
| BraceOpen -> "{" |
| 188 |
| BraceClose -> "}" |
| 189 |
| ParentOpen _ -> "(" |
| 190 |
| ParentClose -> ")" |
| 191 |
| BracketOpen -> "[" |
| 192 |
| BracketClose -> "]" |
| 193 |
| StreamOpen -> "[<" |
| 194 |
| StreamClose -> ">]" |
| 195 |
| Arrow -> "->" |
| 196 |
| Vertical -> "|" |
| 197 |
| Const c -> s_constant c |
| 198 |
| Keyword k -> s_keyword k |
| 199 |
| Binop s -> s |
| 200 |
| Comment s -> "/*" + s + "*--" |
| 201 |
| CommentLine s -> "//" + s |
| 202 |
} |
| 203 |
} |