]> gitweb.factorcode.org Git - factor.git/blob - extra/peg/javascript/parser/parser.factor
core, basis, extra: Remove DOS line endings from files.
[factor.git] / extra / peg / javascript / parser / parser.factor
1 ! Copyright (C) 2008 Chris Double.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel accessors sequences
4 peg peg.ebnf peg.javascript.ast peg.javascript.tokenizer ;
5 IN: peg.javascript.parser
6
7 #! Grammar for JavaScript. Based on OMeta-JS example from:
8 #! http://jarrett.cs.ucla.edu/ometa-js/#JavaScript_Compiler 
9
10 #! The interesting thing about this parser is the mixing of
11 #! a default and non-default tokenizer. The JavaScript tokenizer
12 #! removes all newlines. So when operating on tokens there is no
13 #! need for newline and space skipping in the grammar. But JavaScript
14 #! uses the newline in the 'automatic semicolon insertion' rule. 
15 #!
16 #! If a statement ends in a newline, sometimes the semicolon can be
17 #! skipped. So we define an 'nl' rule using the default tokenizer. 
18 #! This operates a character at a time. Using this 'nl' in the parser
19 #! allows us to detect newlines when we need to for the semicolon
20 #! insertion rule, but ignore it in all other places.
21 EBNF: javascript
22 tokenizer         = default 
23 nl                = "\r" "\n" | "\n"
24
25 tokenizer         = <foreign tokenize-javascript Tok>
26 End               = !(.)
27 Space             = " " | "\t" | "\n" 
28 Spaces            = Space* => [[ ignore ]]
29 Name               = . ?[ ast-name?   ]?   => [[ value>> ]] 
30 Number             = . ?[ ast-number? ]?
31 String             = . ?[ ast-string? ]?
32 RegExp             = . ?[ ast-regexp? ]?   
33 SpacesNoNl         = (!(nl) Space)* => [[ ignore ]]
34
35 Expr               =   OrExpr:e "?" Expr:t ":" Expr:f   => [[ e t f ast-cond-expr boa ]]
36                      | OrExpr:e "=" Expr:rhs            => [[ e rhs ast-set boa ]]
37                      | OrExpr:e "+=" Expr:rhs           => [[ e rhs "+" ast-mset boa ]]
38                      | OrExpr:e "-=" Expr:rhs           => [[ e rhs "-" ast-mset boa ]]
39                      | OrExpr:e "*=" Expr:rhs           => [[ e rhs "*" ast-mset boa ]]
40                      | OrExpr:e "/=" Expr:rhs           => [[ e rhs "/" ast-mset boa ]]
41                      | OrExpr:e "%=" Expr:rhs           => [[ e rhs "%" ast-mset boa ]]
42                      | OrExpr:e "&&=" Expr:rhs          => [[ e rhs "&&" ast-mset boa ]]
43                      | OrExpr:e "||=" Expr:rhs          => [[ e rhs "||" ast-mset boa ]]
44                      | OrExpr:e "^=" Expr:rhs           => [[ e rhs "^" ast-mset boa ]]
45                      | OrExpr:e "&=" Expr:rhs           => [[ e rhs "&" ast-mset boa ]]
46                      | OrExpr:e "|=" Expr:rhs           => [[ e rhs "|" ast-mset boa ]]
47                      | OrExpr:e "<<=" Expr:rhs          => [[ e rhs "<<" ast-mset boa ]]
48                      | OrExpr:e ">>=" Expr:rhs          => [[ e rhs ">>" ast-mset boa ]]
49                      | OrExpr:e ">>>=" Expr:rhs         => [[ e rhs ">>>" ast-mset boa ]]
50                      | OrExpr:e                         => [[ e ]]
51
52 ExprNoIn           =   OrExprNoIn:e "?" ExprNoIn:t ":" ExprNoIn:f => [[ e t f ast-cond-expr boa ]]
53                      | OrExprNoIn:e "=" ExprNoIn:rhs              => [[ e rhs ast-set boa ]]
54                      | OrExprNoIn:e "+=" ExprNoIn:rhs             => [[ e rhs "+" ast-mset boa ]]
55                      | OrExprNoIn:e "-=" ExprNoIn:rhs             => [[ e rhs "-" ast-mset boa ]]
56                      | OrExprNoIn:e "*=" ExprNoIn:rhs             => [[ e rhs "*" ast-mset boa ]]
57                      | OrExprNoIn:e "/=" ExprNoIn:rhs             => [[ e rhs "/" ast-mset boa ]]
58                      | OrExprNoIn:e "%=" ExprNoIn:rhs             => [[ e rhs "%" ast-mset boa ]]
59                      | OrExprNoIn:e "&&=" ExprNoIn:rhs            => [[ e rhs "&&" ast-mset boa ]]
60                      | OrExprNoIn:e "||=" ExprNoIn:rhs            => [[ e rhs "||" ast-mset boa ]]
61                      | OrExprNoIn:e "^=" ExprNoIn:rhs             => [[ e rhs "^" ast-mset boa ]]
62                      | OrExprNoIn:e "&=" ExprNoIn:rhs             => [[ e rhs "&" ast-mset boa ]]
63                      | OrExprNoIn:e "|=" ExprNoIn:rhs             => [[ e rhs "|" ast-mset boa ]]
64                      | OrExprNoIn:e "<<=" ExprNoIn:rhs            => [[ e rhs "<<" ast-mset boa ]]
65                      | OrExprNoIn:e ">>=" ExprNoIn:rhs            => [[ e rhs ">>" ast-mset boa ]]
66                      | OrExprNoIn:e ">>>=" ExprNoIn:rhs           => [[ e rhs ">>>" ast-mset boa ]]
67                      | OrExprNoIn:e                               => [[ e ]]
68
69 OrExpr             =   OrExpr:x "||" AndExpr:y          => [[ x y "||" ast-binop boa ]]
70                      | AndExpr
71 OrExprNoIn         =   OrExprNoIn:x "||" AndExprNoIn:y  => [[ x y "||" ast-binop boa ]]
72                      | AndExprNoIn
73 AndExpr            =   AndExpr:x "&&" BitOrExpr:y       => [[ x y "&&" ast-binop boa ]]
74                      | BitOrExpr
75 AndExprNoIn        =   AndExprNoIn:x "&&" BitOrExprNoIn:y => [[ x y "&&" ast-binop boa ]]
76                      | BitOrExprNoIn
77 BitOrExpr          =   BitOrExpr:x "|" BitXORExpr:y     => [[ x y "|" ast-binop boa ]]
78                      | BitXORExpr
79 BitOrExprNoIn      =   BitOrExprNoIn:x "|" BitXORExprNoIn:y => [[ x y "|" ast-binop boa ]]
80                      | BitXORExprNoIn
81 BitXORExpr         =   BitXORExpr:x "^" BitANDExpr:y    => [[ x y "^" ast-binop boa ]]
82                      | BitANDExpr
83 BitXORExprNoIn     =   BitXORExprNoIn:x "^" BitANDExprNoIn:y => [[ x y "^" ast-binop boa ]]
84                      | BitANDExprNoIn
85 BitANDExpr         =   BitANDExpr:x "&" EqExpr:y        => [[ x y "&" ast-binop boa ]]
86                      | EqExpr
87 BitANDExprNoIn     =   BitANDExprNoIn:x "&" EqExprNoIn:y => [[ x y "&" ast-binop boa ]]
88                      | EqExprNoIn
89 EqExpr             =   EqExpr:x "==" RelExpr:y          => [[ x y "==" ast-binop boa ]]
90                      | EqExpr:x "!=" RelExpr:y          => [[ x y "!=" ast-binop boa ]]
91                      | EqExpr:x "===" RelExpr:y         => [[ x y "===" ast-binop boa ]]
92                      | EqExpr:x "!==" RelExpr:y         => [[ x y "!==" ast-binop boa ]]
93                      | RelExpr
94 EqExprNoIn         =   EqExprNoIn:x "==" RelExprNoIn:y    => [[ x y "==" ast-binop boa ]]
95                      | EqExprNoIn:x "!=" RelExprNoIn:y    => [[ x y "!=" ast-binop boa ]]
96                      | EqExprNoIn:x "===" RelExprNoIn:y   => [[ x y "===" ast-binop boa ]]
97                      | EqExprNoIn:x "!==" RelExprNoIn:y   => [[ x y "!==" ast-binop boa ]]
98                      | RelExprNoIn
99 RelExpr            =   RelExpr:x ">" ShiftExpr:y          => [[ x y ">" ast-binop boa ]]
100                      | RelExpr:x ">=" ShiftExpr:y         => [[ x y ">=" ast-binop boa ]]
101                      | RelExpr:x "<" ShiftExpr:y          => [[ x y "<" ast-binop boa ]]
102                      | RelExpr:x "<=" ShiftExpr:y         => [[ x y "<=" ast-binop boa ]]
103                      | RelExpr:x "instanceof" ShiftExpr:y => [[ x y "instanceof" ast-binop boa ]]
104                      | RelExpr:x "in" ShiftExpr:y         => [[ x y "in" ast-binop boa ]]
105                      | ShiftExpr
106 RelExprNoIn        =   RelExprNoIn:x ">" ShiftExpr:y          => [[ x y ">" ast-binop boa ]]
107                      | RelExprNoIn:x ">=" ShiftExpr:y         => [[ x y ">=" ast-binop boa ]]
108                      | RelExprNoIn:x "<" ShiftExpr:y          => [[ x y "<" ast-binop boa ]]
109                      | RelExprNoIn:x "<=" ShiftExpr:y         => [[ x y "<=" ast-binop boa ]]
110                      | RelExprNoIn:x "instanceof" ShiftExpr:y => [[ x y "instanceof" ast-binop boa ]]
111                      | ShiftExpr
112 ShiftExpr          =   ShiftExpr:x "<<" AddExpr:y       => [[ x y "<<" ast-binop boa ]]
113                      | ShiftExpr:x ">>>" AddExpr:y      => [[ x y ">>>" ast-binop boa ]]
114                      | ShiftExpr:x ">>" AddExpr:y       => [[ x y ">>" ast-binop boa ]]
115                      | AddExpr
116 AddExpr            =   AddExpr:x "+" MulExpr:y          => [[ x y "+" ast-binop boa ]]
117                      | AddExpr:x "-" MulExpr:y          => [[ x y "-" ast-binop boa ]]
118                      | MulExpr
119 MulExpr            =   MulExpr:x "*" Unary:y            => [[ x y "*" ast-binop boa ]]
120                      | MulExpr:x "/" Unary:y            => [[ x y "/" ast-binop boa ]]
121                      | MulExpr:x "%" Unary:y            => [[ x y "%" ast-binop boa ]]
122                      | Unary
123 Unary              =   "-" Unary:p                      => [[ p "-" ast-unop boa ]]
124                      | "+" Unary:p                      => [[ p ]]
125                      | "++" Unary:p                     => [[ p "++" ast-preop boa ]]
126                      | "--" Unary:p                     => [[ p "--" ast-preop boa ]]
127                      | "!" Unary:p                      => [[ p "!" ast-unop boa ]]
128                      | "typeof" Unary:p                 => [[ p "typeof" ast-unop boa ]]
129                      | "void" Unary:p                   => [[ p "void" ast-unop boa ]]
130                      | "delete" Unary:p                 => [[ p "delete" ast-unop boa ]]
131                      | Postfix
132 Postfix            =   PrimExpr:p SpacesNoNl "++"       => [[ p "++" ast-postop boa ]]
133                      | PrimExpr:p SpacesNoNl "--"       => [[ p "--" ast-postop boa ]]
134                      | PrimExpr
135 Args               =   (Expr ("," Expr => [[ second ]])* => [[ first2 swap prefix ]])?
136 PrimExpr           =   PrimExpr:p "[" Expr:i "]"             => [[ i p ast-getp boa ]]
137                      | PrimExpr:p "." Name:m "(" Args:as ")" => [[ m p as ast-send boa ]]
138                      | PrimExpr:p "." Name:f                 => [[ f p ast-getp boa ]]
139                      | PrimExpr:p "(" Args:as ")"            => [[ p as ast-call boa ]]
140                      | PrimExprHd
141 PrimExprHd         =   "(" Expr:e ")"                        => [[ e ]]
142                      | "this"                                => [[ ast-this boa ]]
143                      | Name                                  => [[ ast-get boa ]]
144                      | Number
145                      | String
146                      | RegExp
147                      | "function" FuncRest:fr                => [[ fr ]]
148                      | "new" PrimExpr:n "(" Args:as ")"      => [[ n as ast-new boa ]]
149                      | "new" PrimExpr:n                      => [[ n f  ast-new boa ]]
150                      | "[" Args:es "]"                       => [[ es ast-array boa ]]
151                      | Json
152 JsonBindings       = (JsonBinding ("," JsonBinding => [[ second ]])* => [[ first2 swap prefix ]])?
153 Json               = "{" JsonBindings:bs "}"                  => [[ bs ast-json boa ]]
154 JsonBinding        = JsonPropName:n ":" Expr:v               => [[ n v ast-binding boa ]]
155 JsonPropName       = Name | Number | String | RegExp
156 Formal             = Spaces Name
157 Formals            = (Formal ("," Formal => [[ second ]])*  => [[ first2 swap prefix ]])?
158 FuncRest           = "(" Formals:fs ")" "{" SrcElems:body "}" => [[ fs body ast-func boa ]]
159 Sc                 = SpacesNoNl (nl | &("}") | End)| ";"
160 Binding            =   Name:n "=" Expr:v                      => [[ n v ast-var boa ]]
161                      | Name:n                                 => [[ n "undefined" ast-get boa ast-var boa ]]
162 Block              = "{" SrcElems:ss "}"                      => [[ ss ]]
163 Bindings           = (Binding ("," Binding => [[ second ]])* => [[ first2 swap prefix ]])?
164 For1               =   "var" Bindings => [[ second ]] 
165                      | ExprNoIn 
166                      | Spaces => [[ "undefined" ast-get boa ]] 
167 For2               =   Expr
168                      | Spaces => [[ "true" ast-get boa ]] 
169 For3               =   Expr
170                      | Spaces => [[ "undefined" ast-get boa ]] 
171 ForIn1             =   "var" Name:n => [[ n "undefined" ast-get boa ast-var boa ]]
172                      | PrimExprHd
173 Switch1            =   "case" Expr:c ":" SrcElems:cs => [[ c cs ast-case boa ]]
174                      | "default" ":" SrcElems:cs => [[ cs ast-default boa ]]  
175 SwitchBody         = Switch1*
176 Finally            =   "finally" Block:b => [[ b ]]
177                      | Spaces => [[ "undefined" ast-get boa ]]
178 Stmt               =   Block                     
179                      | "var" Bindings:bs Sc                   => [[ bs ast-begin boa ]]
180                      | "if" "(" Expr:c ")" Stmt:t "else" Stmt:f => [[ c t f ast-if boa ]]
181                      | "if" "(" Expr:c ")" Stmt:t               => [[ c t "undefined" ast-get boa ast-if boa ]]
182                      | "while" "(" Expr:c ")" Stmt:s            => [[ c s ast-while boa ]]
183                      | "do" Stmt:s "while" "(" Expr:c ")" Sc    => [[ s c ast-do-while boa ]]
184                      | "for" "(" For1:i ";" For2:c ";" For3:u ")" Stmt:s => [[ i c u s ast-for boa ]]
185                      | "for" "(" ForIn1:v "in" Expr:e ")" Stmt:s => [[ v e s ast-for-in boa ]]
186                      | "switch" "(" Expr:e ")" "{" SwitchBody:cs "}" => [[ e cs ast-switch boa ]]
187                      | "break" Sc                                    => [[ ast-break boa ]]
188                      | "continue" Sc                                 => [[ ast-continue boa ]]
189                      | "throw" SpacesNoNl Expr:e Sc                  => [[ e ast-throw boa ]]
190                      | "try" Block:t "catch" "(" Name:e ")" Block:c Finally:f => [[ t e c f ast-try boa ]]
191                      | "return" Expr:e Sc                            => [[ e ast-return boa ]]
192                      | "return" Sc                                   => [[ "undefined" ast-get boa ast-return boa ]]
193                      | "with" "(" Expr:e ")" Stmt:b                  => [[ e b ast-with boa ]]
194                      | Expr:e Sc                                     => [[ e ]]
195                      | ";"                                           => [[ "undefined" ast-get boa ]]
196 SrcElem            =   "function" Name:n FuncRest:f                  => [[ n f ast-var boa ]]
197                      | Stmt
198 SrcElems           = SrcElem*                                      => [[ ast-begin boa ]]
199 TopLevel           = SrcElems Spaces                               
200 ;EBNF