| 1 |
.boilerplate{{{/+ |
|---|
| 2 |
Copyright (c) 2006 Eric Anderton |
|---|
| 3 |
|
|---|
| 4 |
Permission is hereby granted, free of charge, to any person |
|---|
| 5 |
obtaining a copy of this software and associated documentation |
|---|
| 6 |
files (the "Software"), to deal in the Software without |
|---|
| 7 |
restriction, including without limitation the rights to use, |
|---|
| 8 |
copy, modify, merge, publish, distribute, sublicense, and/or |
|---|
| 9 |
sell copies of the Software, and to permit persons to whom the |
|---|
| 10 |
Software is furnished to do so, subject to the following |
|---|
| 11 |
conditions: |
|---|
| 12 |
|
|---|
| 13 |
The above copyright notice and this permission notice shall be |
|---|
| 14 |
included in all copies or substantial portions of the Software. |
|---|
| 15 |
|
|---|
| 16 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|---|
| 17 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
|---|
| 18 |
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|---|
| 19 |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|---|
| 20 |
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
|---|
| 21 |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|---|
| 22 |
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
|---|
| 23 |
OTHER DEALINGS IN THE SOFTWARE. |
|---|
| 24 |
+/}}} |
|---|
| 25 |
|
|---|
| 26 |
# Enki Grammar Definition (self-hosting frontend) |
|---|
| 27 |
.module("enki.EnkiParser"); |
|---|
| 28 |
|
|---|
| 29 |
.import("enki.EnkiBackend"); |
|---|
| 30 |
.import("enki.Rule"); |
|---|
| 31 |
.import("enki.Expression"); |
|---|
| 32 |
.import("enki.Directive"); |
|---|
| 33 |
|
|---|
| 34 |
.define("bool","eoi","true","End of Input"); |
|---|
| 35 |
.define("bool","eol","true","End of Line"); |
|---|
| 36 |
.define("bool","err","true","Error"); |
|---|
| 37 |
.define("String","letter","true","Letter"); |
|---|
| 38 |
.define("String","digit","true","Digit"); |
|---|
| 39 |
.define("String","hexdigit","true","Hexdigit"); |
|---|
| 40 |
.define("String","any","true","any"); |
|---|
| 41 |
.define("String","newline","true","Newline"); |
|---|
| 42 |
.define("String","sp","true","Space(s)"); |
|---|
| 43 |
.define("String","ws","false","Whitespace"); |
|---|
| 44 |
|
|---|
| 45 |
.parsetype("String"); |
|---|
| 46 |
.utf("8"); |
|---|
| 47 |
|
|---|
| 48 |
.baseclass("BaseEnkiParser"); |
|---|
| 49 |
.classname("EnkiParser"); |
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
WS |
|---|
| 53 |
::= ws [ ( SlashSlashComment | SlashStarComment) WS]; |
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
Syntax |
|---|
| 57 |
= void createSyntax(SyntaxLine[] lines) |
|---|
| 58 |
::= WS { ( Rule:~lines | Comment:~lines | Directive:~lines) WS} eoi; |
|---|
| 59 |
|
|---|
| 60 |
# |
|---|
| 61 |
# Rule and Predicate |
|---|
| 62 |
# |
|---|
| 63 |
|
|---|
| 64 |
Rule |
|---|
| 65 |
= new Rule(String name,RulePredicate pred,Expression expr,RuleDecl decl) |
|---|
| 66 |
::= Identifier:name WS [ RuleDecl:decl WS] [ RulePredicate:pred] WS "::=" WS Expression:expr WS ";"; |
|---|
| 67 |
|
|---|
| 68 |
RuleDecl |
|---|
| 69 |
= new RuleDecl(Param[] params) |
|---|
| 70 |
::= ParamsExpr:params; |
|---|
| 71 |
|
|---|
| 72 |
RulePredicate |
|---|
| 73 |
= RulePredicate pred |
|---|
| 74 |
::= "=" WS ( ClassPredicate:pred | FunctionPredicate:pred | BindingPredicate:pred); |
|---|
| 75 |
|
|---|
| 76 |
ClassPredicate |
|---|
| 77 |
= new ClassPredicate(String name,Param[] params) |
|---|
| 78 |
::= "new" WS Identifier:name WS ParamsExpr:params; |
|---|
| 79 |
|
|---|
| 80 |
FunctionPredicate |
|---|
| 81 |
= new FunctionPredicate(Param decl,Param[] params) |
|---|
| 82 |
::= ExplicitParam:decl WS ParamsExpr:params; |
|---|
| 83 |
|
|---|
| 84 |
BindingPredicate |
|---|
| 85 |
= new BindingPredicate(Param param) |
|---|
| 86 |
::= Param:param; |
|---|
| 87 |
|
|---|
| 88 |
ParamsExpr |
|---|
| 89 |
= Param[] params |
|---|
| 90 |
::= "(" WS [ Param:~params WS { "," WS Param:~params WS}] ")"; |
|---|
| 91 |
|
|---|
| 92 |
Param |
|---|
| 93 |
= Param param |
|---|
| 94 |
::= ExplicitParam:param | WeakParam:param; |
|---|
| 95 |
|
|---|
| 96 |
WeakParam |
|---|
| 97 |
= new Param(String name) |
|---|
| 98 |
::= Identifier:name; |
|---|
| 99 |
|
|---|
| 100 |
ExplicitParam |
|---|
| 101 |
= new Param(bool isArray,String type,String name) |
|---|
| 102 |
::= Identifier:type WS [ "[]":isArray Brackets WS] Identifier:name; |
|---|
| 103 |
|
|---|
| 104 |
Brackets |
|---|
| 105 |
::= [ "[]" Brackets]; |
|---|
| 106 |
# |
|---|
| 107 |
# Expressions |
|---|
| 108 |
# |
|---|
| 109 |
|
|---|
| 110 |
Expression |
|---|
| 111 |
= new Expression(Term[] terms) |
|---|
| 112 |
::= Term:~terms WS { "|" WS Term:~terms WS}; |
|---|
| 113 |
|
|---|
| 114 |
Term |
|---|
| 115 |
= SubExpression[] factors |
|---|
| 116 |
::= SubExpression:~factors WS { SubExpression:~factors WS}; |
|---|
| 117 |
|
|---|
| 118 |
SubExpression |
|---|
| 119 |
= SubExpression expr |
|---|
| 120 |
::= Production:expr | Substitution:expr | Terminal:expr | Range:expr | Regexp:expr | GroupExpr:expr | OptionalExpr:expr | ZeroOrMoreExpr:expr | NegateExpr:expr | TestExpr:expr | LiteralExpr:expr | CustomTerminal:expr; |
|---|
| 121 |
|
|---|
| 122 |
Production |
|---|
| 123 |
= new Production(String name,Binding binding,ProductionArg[] args) |
|---|
| 124 |
::= Identifier:name WS [ "!(" WS ProductionArg:~args { WS "," WS ProductionArg:~args} ")"] [ Binding:binding]; |
|---|
| 125 |
|
|---|
| 126 |
ProductionArg |
|---|
| 127 |
= ProductionArg arg |
|---|
| 128 |
::= StringProductionArg:arg | BindingProductionArg:arg; |
|---|
| 129 |
|
|---|
| 130 |
StringProductionArg |
|---|
| 131 |
= new StringProductionArg(String value) |
|---|
| 132 |
::= String:value; |
|---|
| 133 |
|
|---|
| 134 |
BindingProductionArg |
|---|
| 135 |
= new BindingProductionArg(String value) |
|---|
| 136 |
::= Identifier:value; |
|---|
| 137 |
|
|---|
| 138 |
Substitution |
|---|
| 139 |
= new Substitution(String name,Binding binding) |
|---|
| 140 |
::= "." Identifier:name WS [ Binding:binding]; |
|---|
| 141 |
|
|---|
| 142 |
GroupExpr |
|---|
| 143 |
= new GroupExpr(Expression expr,Binding binding) |
|---|
| 144 |
::= "(" WS Expression:expr WS ")" WS [ Binding:binding]; |
|---|
| 145 |
|
|---|
| 146 |
OptionalExpr |
|---|
| 147 |
= new OptionalExpr(Expression expr,Binding binding) |
|---|
| 148 |
::= "[" WS Expression:expr WS "]" WS [ Binding:binding]; |
|---|
| 149 |
|
|---|
| 150 |
ZeroOrMoreExpr |
|---|
| 151 |
= new ZeroOrMoreExpr(Expression expr,Binding binding,SubExpression term) |
|---|
| 152 |
::= "{" WS Expression:expr WS "}" WS [ Binding:binding WS] [ SubExpression:term]; |
|---|
| 153 |
|
|---|
| 154 |
Terminal |
|---|
| 155 |
= new Terminal(String text,Binding binding) |
|---|
| 156 |
::= String:text WS [ Binding:binding]; |
|---|
| 157 |
|
|---|
| 158 |
Range |
|---|
| 159 |
= new Range(String start,String end,Binding binding) |
|---|
| 160 |
::= HexExpr:start WS [ "-" WS HexExpr:end WS] [ Binding:binding]; |
|---|
| 161 |
|
|---|
| 162 |
Regexp |
|---|
| 163 |
= new Regexp(String text,Binding binding) |
|---|
| 164 |
::= ( "r" String:text | "`" { any}:text "`") WS [ Binding:binding]; |
|---|
| 165 |
|
|---|
| 166 |
NegateExpr |
|---|
| 167 |
= new Negate(SubExpression expr) |
|---|
| 168 |
::= "!" WS SubExpression:expr; |
|---|
| 169 |
|
|---|
| 170 |
TestExpr |
|---|
| 171 |
= new Test(SubExpression expr) |
|---|
| 172 |
::= "/" WS SubExpression:expr; |
|---|
| 173 |
|
|---|
| 174 |
LiteralExpr |
|---|
| 175 |
= new LiteralExpr(String name,Binding binding,ProductionArg[] args) |
|---|
| 176 |
::= "@" Identifier:name WS [ "!(" WS ProductionArg:~args { WS "," WS ProductionArg:~args} ")"] [ Binding:binding]; |
|---|
| 177 |
|
|---|
| 178 |
CustomTerminal |
|---|
| 179 |
= new CustomTerminal(String name,Binding binding) |
|---|
| 180 |
::= "&" Identifier:name WS [ Binding:binding]; |
|---|
| 181 |
|
|---|
| 182 |
Binding |
|---|
| 183 |
= new Binding(bool isConcat,String name) |
|---|
| 184 |
::= ":" WS [ "~"]:isConcat WS Identifier:name; |
|---|
| 185 |
|
|---|
| 186 |
Identifier |
|---|
| 187 |
= String value |
|---|
| 188 |
::= ( IdentifierStartChar { IdentifierChar}):value ; |
|---|
| 189 |
|
|---|
| 190 |
IdentifierStartChar |
|---|
| 191 |
= String text |
|---|
| 192 |
::= ( letter | "_"):text ; |
|---|
| 193 |
|
|---|
| 194 |
IdentifierChar |
|---|
| 195 |
= String text |
|---|
| 196 |
::= ( letter | digit | "_" | "."):text ; |
|---|
| 197 |
|
|---|
| 198 |
String |
|---|
| 199 |
= String text |
|---|
| 200 |
::= ( "\"" | "\'"):~delim { AnyChar}:text.delim; |
|---|
| 201 |
|
|---|
| 202 |
HexExpr |
|---|
| 203 |
= String text |
|---|
| 204 |
::= "#" ( hexdigit hexdigit [ hexdigit hexdigit [ hexdigit hexdigit hexdigit hexdigit [ hexdigit hexdigit hexdigit hexdigit hexdigit hexdigit hexdigit hexdigit]]]):text ; |
|---|
| 205 |
|
|---|
| 206 |
AnyChar |
|---|
| 207 |
= String value |
|---|
| 208 |
::= [ "\\":~value] any:~value; |
|---|
| 209 |
|
|---|
| 210 |
# |
|---|
| 211 |
# Comments |
|---|
| 212 |
# |
|---|
| 213 |
|
|---|
| 214 |
|
|---|
| 215 |
Comment |
|---|
| 216 |
= new Comment(String text) |
|---|
| 217 |
::= PoundComment:text | SlashSlashComment:text | SlashStarComment:text; |
|---|
| 218 |
|
|---|
| 219 |
PoundComment |
|---|
| 220 |
= String text |
|---|
| 221 |
::= "#" { any}:text eol; |
|---|
| 222 |
|
|---|
| 223 |
SlashSlashComment |
|---|
| 224 |
= String text |
|---|
| 225 |
::= "\x2F\x2F" { any}:text eol; |
|---|
| 226 |
|
|---|
| 227 |
SlashStarComment |
|---|
| 228 |
= String text |
|---|
| 229 |
::= "\x2F\x2A" { any}:text "\x2A\x2F"; |
|---|
| 230 |
|
|---|
| 231 |
# |
|---|
| 232 |
# Directives |
|---|
| 233 |
# |
|---|
| 234 |
|
|---|
| 235 |
Directive |
|---|
| 236 |
= Directive dir |
|---|
| 237 |
::= "." ( ImportDirective:~dir | BaseClassDirective:~dir | ClassnameDirective:~dir | DefineDirective:~dir | IncludeDirective:~dir | AliasDirective:~dir | ModuleDirective:~dir | CodeDirective:~dir | TypelibDirective:~dir | ParseTypeDirective:~dir | BoilerplateDirective:~dir | HeaderDirective:~dir | UTFDirective:~dir); |
|---|
| 238 |
|
|---|
| 239 |
ImportDirective |
|---|
| 240 |
= new ImportDirective(String imp) |
|---|
| 241 |
::= "import" WS "(" WS DirectiveArg:imp WS ")" WS ";"; |
|---|
| 242 |
|
|---|
| 243 |
BaseClassDirective |
|---|
| 244 |
= new BaseClassDirective(String name) |
|---|
| 245 |
::= "baseclass" WS "(" WS DirectiveArg:name WS ")" WS ";"; |
|---|
| 246 |
|
|---|
| 247 |
ClassnameDirective |
|---|
| 248 |
= new ClassnameDirective(String name) |
|---|
| 249 |
::= "classname" WS "(" WS DirectiveArg:name WS ")" WS ";"; |
|---|
| 250 |
|
|---|
| 251 |
DefineDirective |
|---|
| 252 |
= new DefineDirective(String returnType,String name,bool isTerminal,String description) |
|---|
| 253 |
::= "define" WS "(" WS DirectiveArg:returnType WS "," WS DirectiveArg:name WS "," WS DirectiveArg:isTerminal WS [ "," WS DirectiveArg:description WS] ")" WS ";"; |
|---|
| 254 |
|
|---|
| 255 |
IncludeDirective |
|---|
| 256 |
= new IncludeDirective(String filename) |
|---|
| 257 |
::= "include" WS "(" WS String:filename WS ")" WS ";"; |
|---|
| 258 |
|
|---|
| 259 |
AliasDirective |
|---|
| 260 |
= new AliasDirective(String rule,String ruleAlias) |
|---|
| 261 |
::= "alias" WS "(" WS DirectiveArg:rule WS "," WS DirectiveArg:ruleAlias WS ")" WS ";"; |
|---|
| 262 |
|
|---|
| 263 |
ModuleDirective |
|---|
| 264 |
= new ModuleDirective(String moduleName) |
|---|
| 265 |
::= "module" WS "(" WS DirectiveArg:moduleName WS ")" WS ";"; |
|---|
| 266 |
|
|---|
| 267 |
CodeDirective |
|---|
| 268 |
= new CodeDirective(String code) |
|---|
| 269 |
::= "code" WS "{{{" { any}:code "}}}"; |
|---|
| 270 |
|
|---|
| 271 |
TypelibDirective |
|---|
| 272 |
= new TypelibDirective(String importName) |
|---|
| 273 |
::= "typelib" WS "(" WS DirectiveArg:importName WS ")" WS ";"; |
|---|
| 274 |
|
|---|
| 275 |
ParseTypeDirective |
|---|
| 276 |
= new ParseTypeDirective(String typeName) |
|---|
| 277 |
::= "parsetype" WS "(" WS DirectiveArg:typeName WS ")" WS ";"; |
|---|
| 278 |
|
|---|
| 279 |
BoilerplateDirective |
|---|
| 280 |
= new BoilerplateDirective(String code) |
|---|
| 281 |
::= "boilerplate" WS "{{{" { any}:code "}}}"; |
|---|
| 282 |
|
|---|
| 283 |
HeaderDirective |
|---|
| 284 |
= new HeaderDirective(String code) |
|---|
| 285 |
::= "header" WS "{{{" { any}:code "}}}"; |
|---|
| 286 |
|
|---|
| 287 |
UTFDirective |
|---|
| 288 |
= new UTFDirective(String value) |
|---|
| 289 |
::= "utf" WS "(" WS DirectiveArg:value WS ")" WS ";"; |
|---|
| 290 |
|
|---|
| 291 |
DirectiveArg |
|---|
| 292 |
= String arg |
|---|
| 293 |
::= Identifier:arg | String:arg; |
|---|