Javascript parser now works on token sequence
parent
258951d954
commit
4050ebcbde
|
@ -410,7 +410,7 @@ M: ebnf-var (transform) ( ast -- parser )
|
||||||
parser>> (transform) ;
|
parser>> (transform) ;
|
||||||
|
|
||||||
M: ebnf-terminal (transform) ( ast -- parser )
|
M: ebnf-terminal (transform) ( ast -- parser )
|
||||||
symbol>> token ;
|
symbol>> [ token ] keep [ = ] curry satisfy 2choice ;
|
||||||
|
|
||||||
M: ebnf-foreign (transform) ( ast -- parser )
|
M: ebnf-foreign (transform) ( ast -- parser )
|
||||||
dup word>> search
|
dup word>> search
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
! Copyright (C) 2007 Chris Double.
|
! Copyright (C) 2007 Chris Double.
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: kernel arrays strings math.parser sequences sequences.deep
|
USING: kernel arrays strings math.parser sequences sequences.deep
|
||||||
peg peg.ebnf peg.parsers memoize namespaces math ;
|
peg peg.ebnf peg.parsers memoize namespaces math accessors ;
|
||||||
IN: peg.javascript
|
IN: peg.javascript
|
||||||
|
|
||||||
#! Grammar for JavaScript. Based on OMeta-JS example from:
|
#! Grammar for JavaScript. Based on OMeta-JS example from:
|
||||||
|
@ -81,7 +81,7 @@ Keyword = ("break"
|
||||||
| "var"
|
| "var"
|
||||||
| "void"
|
| "void"
|
||||||
| "while"
|
| "while"
|
||||||
| "with") => [[ ast-keyword boa ]]
|
| "with")
|
||||||
Name = !(Keyword) (iName):n => [[ n ast-name boa ]]
|
Name = !(Keyword) (iName):n => [[ n ast-name boa ]]
|
||||||
Number = Digits:ws '.' Digits:fs => [[ ws "." fs 3array concat >string string>number ast-number boa ]]
|
Number = Digits:ws '.' Digits:fs => [[ ws "." fs 3array concat >string string>number ast-number boa ]]
|
||||||
| Digits => [[ >string string>number ast-number boa ]]
|
| Digits => [[ >string string>number ast-number boa ]]
|
||||||
|
@ -105,61 +105,11 @@ Toks = (Tok)* Spaces
|
||||||
;EBNF
|
;EBNF
|
||||||
|
|
||||||
EBNF: javascript
|
EBNF: javascript
|
||||||
Letter = [a-zA-Z]
|
Space = " " | "\t" | "\n"
|
||||||
Digit = [0-9]
|
|
||||||
Digits = (Digit)+
|
|
||||||
SingleLineComment = "//" (!("\n") .)* "\n" => [[ ignore ]]
|
|
||||||
MultiLineComment = "/*" (!("*/") .)* "*/" => [[ ignore ]]
|
|
||||||
Space = " " | "\t" | "\n" | SingleLineComment | MultiLineComment
|
|
||||||
Spaces = (Space)* => [[ ignore ]]
|
Spaces = (Space)* => [[ ignore ]]
|
||||||
NameFirst = Letter | "$" | "_"
|
Name = . ?[ ast-name? ]? => [[ value>> ]]
|
||||||
NameRest = NameFirst | Digit
|
Number = . ?[ ast-number? ]? => [[ value>> ]]
|
||||||
iName = NameFirst (NameRest)* => [[ first2 swap prefix >string ]]
|
String = . ?[ ast-string? ]? => [[ value>> ]]
|
||||||
Keyword = ("break"
|
|
||||||
| "case"
|
|
||||||
| "catch"
|
|
||||||
| "continue"
|
|
||||||
| "default"
|
|
||||||
| "delete"
|
|
||||||
| "do"
|
|
||||||
| "else"
|
|
||||||
| "finally"
|
|
||||||
| "for"
|
|
||||||
| "function"
|
|
||||||
| "if"
|
|
||||||
| "in"
|
|
||||||
| "instanceof"
|
|
||||||
| "new"
|
|
||||||
| "return"
|
|
||||||
| "switch"
|
|
||||||
| "this"
|
|
||||||
| "throw"
|
|
||||||
| "try"
|
|
||||||
| "typeof"
|
|
||||||
| "var"
|
|
||||||
| "void"
|
|
||||||
| "while"
|
|
||||||
| "with") => [[ ast-keyword boa ]]
|
|
||||||
Name = !(Keyword) (iName):n => [[ n ast-name boa ]]
|
|
||||||
Number = Digits:ws '.' Digits:fs => [[ ws "." fs 3array concat >string string>number ast-number boa ]]
|
|
||||||
| Digits => [[ >string string>number ast-number boa ]]
|
|
||||||
|
|
||||||
EscapeChar = "\\n" => [[ 10 ]]
|
|
||||||
| "\\r" => [[ 13 ]]
|
|
||||||
| "\\t" => [[ 9 ]]
|
|
||||||
StringChars1 = (EscapeChar | !('"""') .)* => [[ >string ]]
|
|
||||||
StringChars2 = (EscapeChar | !('"') .)* => [[ >string ]]
|
|
||||||
StringChars3 = (EscapeChar | !("'") .)* => [[ >string ]]
|
|
||||||
Str = '"""' StringChars1:cs '"""' => [[ cs ast-string boa ]]
|
|
||||||
| '"' StringChars2:cs '"' => [[ cs ast-string boa ]]
|
|
||||||
| "'" StringChars3:cs "'" => [[ cs ast-string boa ]]
|
|
||||||
Special = "(" | ")" | "{" | "}" | "[" | "]" | "," | ";"
|
|
||||||
| "?" | ":" | "!==" | "~=" | "===" | "==" | "=" | ">="
|
|
||||||
| ">" | "<=" | "<" | "++" | "+=" | "+" | "--" | "-="
|
|
||||||
| "-" | "*=" | "*" | "/=" | "/" | "%=" | "%" | "&&="
|
|
||||||
| "&&" | "||=" | "||" | "." | "!"
|
|
||||||
Tok = Spaces (Name | Keyword | Number | Str | Special )
|
|
||||||
Toks = (Tok)* Spaces
|
|
||||||
SpacesNoNl = (!("\n") Space)* => [[ ignore ]]
|
SpacesNoNl = (!("\n") Space)* => [[ ignore ]]
|
||||||
|
|
||||||
Expr = OrExpr:e "?" Expr:t ":" Expr:f => [[ e t f ast-cond-expr boa ]]
|
Expr = OrExpr:e "?" Expr:t ":" Expr:f => [[ e t f ast-cond-expr boa ]]
|
||||||
|
@ -214,7 +164,7 @@ PrimExprHd = "(" Expr:e ")" => [[ e ]]
|
||||||
| "this" => [[ ast-this boa ]]
|
| "this" => [[ ast-this boa ]]
|
||||||
| Name => [[ ast-get boa ]]
|
| Name => [[ ast-get boa ]]
|
||||||
| Number => [[ ast-number boa ]]
|
| Number => [[ ast-number boa ]]
|
||||||
| Str => [[ ast-string boa ]]
|
| String => [[ ast-string boa ]]
|
||||||
| "function" FuncRest:fr => [[ fr ]]
|
| "function" FuncRest:fr => [[ fr ]]
|
||||||
| "new" Name:n "(" Args:as ")" => [[ n as ast-new boa ]]
|
| "new" Name:n "(" Args:as ")" => [[ n as ast-new boa ]]
|
||||||
| "[" Args:es "]" => [[ es ast-array boa ]]
|
| "[" Args:es "]" => [[ es ast-array boa ]]
|
||||||
|
@ -222,7 +172,7 @@ PrimExprHd = "(" Expr:e ")" => [[ e ]]
|
||||||
JsonBindings = JsonBinding ("," JsonBinding)* => [[ first2 swap prefix ]]
|
JsonBindings = JsonBinding ("," JsonBinding)* => [[ first2 swap prefix ]]
|
||||||
Json = "{" JsonBindings:bs "}" => [[ bs ast-json boa ]]
|
Json = "{" JsonBindings:bs "}" => [[ bs ast-json boa ]]
|
||||||
JsonBinding = JsonPropName:n ":" Expr:v => [[ n v ast-binding boa ]]
|
JsonBinding = JsonPropName:n ":" Expr:v => [[ n v ast-binding boa ]]
|
||||||
JsonPropName = Name | Number | Str
|
JsonPropName = Name | Number | String
|
||||||
Formal = Spaces Name
|
Formal = Spaces Name
|
||||||
Formals = Formal ("," Formal)* => [[ first2 swap prefix ]]
|
Formals = Formal ("," Formal)* => [[ first2 swap prefix ]]
|
||||||
FuncRest = "(" Formals:fs ")" "{" SrcElems:body "}" => [[ fs body ast-func boa ]]
|
FuncRest = "(" Formals:fs ")" "{" SrcElems:body "}" => [[ fs body ast-func boa ]]
|
||||||
|
|
Loading…
Reference in New Issue