From 4050ebcbde098f0b09a34a2123a12dbdc78d134c Mon Sep 17 00:00:00 2001
From: Chris Double <chris@bethia.(none)>
Date: Tue, 17 Jun 2008 23:42:28 +1200
Subject: [PATCH] Javascript parser now works on token sequence

---
 extra/peg/ebnf/ebnf.factor             |  2 +-
 extra/peg/javascript/javascript.factor | 66 ++++----------------------
 2 files changed, 9 insertions(+), 59 deletions(-)

diff --git a/extra/peg/ebnf/ebnf.factor b/extra/peg/ebnf/ebnf.factor
index 215eabdd37..36b3742b64 100644
--- a/extra/peg/ebnf/ebnf.factor
+++ b/extra/peg/ebnf/ebnf.factor
@@ -410,7 +410,7 @@ M: ebnf-var (transform) ( ast -- parser )
   parser>> (transform) ;
 
 M: ebnf-terminal (transform) ( ast -- parser )
-  symbol>> token ;
+  symbol>> [ token ] keep [ = ] curry satisfy 2choice ;
 
 M: ebnf-foreign (transform) ( ast -- parser )
   dup word>> search
diff --git a/extra/peg/javascript/javascript.factor b/extra/peg/javascript/javascript.factor
index 3db962420a..c9bef2f6d3 100644
--- a/extra/peg/javascript/javascript.factor
+++ b/extra/peg/javascript/javascript.factor
@@ -1,7 +1,7 @@
 ! Copyright (C) 2007 Chris Double.
 ! See http://factorcode.org/license.txt for BSD license.
 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
 
 #! Grammar for JavaScript. Based on OMeta-JS example from:
@@ -81,7 +81,7 @@ Keyword           =  ("break"
                     | "var"
                     | "void"
                     | "while"
-                    | "with") => [[ ast-keyword boa ]]
+                    | "with") 
 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 ]]  
@@ -105,61 +105,11 @@ Toks               = (Tok)* Spaces
 ;EBNF
 
 EBNF: javascript
-Letter            = [a-zA-Z]
-Digit             = [0-9]
-Digits            = (Digit)+
-SingleLineComment = "//" (!("\n") .)* "\n" => [[ ignore ]]
-MultiLineComment  = "/*" (!("*/") .)* "*/" => [[ ignore ]]
-Space             = " " | "\t" | "\n" | SingleLineComment | MultiLineComment
+Space             = " " | "\t" | "\n" 
 Spaces            = (Space)* => [[ ignore ]]
-NameFirst         = Letter | "$" | "_"
-NameRest          = NameFirst | Digit
-iName             = NameFirst (NameRest)* => [[ first2 swap prefix >string ]]
-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 
+Name               = . ?[ ast-name?   ]?   => [[ value>> ]] 
+Number             = . ?[ ast-number? ]?   => [[ value>> ]]
+String             = . ?[ ast-string? ]?   => [[ value>> ]]
 SpacesNoNl         = (!("\n") Space)* => [[ ignore ]]
 
 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 ]]
                      | Name                                  => [[ ast-get boa ]]
                      | Number                                => [[ ast-number boa ]]
-                     | Str                                   => [[ ast-string boa ]]
+                     | String                                => [[ ast-string boa ]]
                      | "function" FuncRest:fr                => [[ fr ]]
                      | "new" Name:n "(" Args:as ")"          => [[ n as ast-new boa ]]
                      | "[" Args:es "]"                       => [[ es ast-array boa ]]
@@ -222,7 +172,7 @@ PrimExprHd         =   "(" Expr:e ")"                        => [[ e ]]
 JsonBindings        = JsonBinding ("," JsonBinding)*          => [[ first2 swap prefix ]]
 Json               = "{" JsonBindings:bs "}"                  => [[ bs ast-json boa ]]
 JsonBinding        = JsonPropName:n ":" Expr:v               => [[ n v ast-binding boa ]]
-JsonPropName       = Name | Number | Str
+JsonPropName       = Name | Number | String
 Formal             = Spaces Name
 Formals            = Formal ("," Formal)*                    => [[ first2 swap prefix ]]
 FuncRest           = "(" Formals:fs ")" "{" SrcElems:body "}" => [[ fs body ast-func boa ]]