-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: move arraiParsers to a separated file
- Loading branch information
1 parent
018fa19
commit 6057f1c
Showing
6 changed files
with
191 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package main | ||
|
||
//go:generate go run tools/parser/generate_parser.go syntax/arrai.wbnf syntax/parser.go | ||
//go:generate goimports -w syntax/parser.go | ||
|
||
func main() { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
expr -> C* amp="&"* @ C* arrow=( | ||
nest | | ||
unnest | | ||
ARROW @ | | ||
binding="->" C* "\\" C* IDENT C* %%bind C* @ | | ||
binding="->" C* %%bind @ | ||
)* C* | ||
> C* @:binop=("with" | "without") C* | ||
> C* @:binop="||" C* | ||
> C* @:binop="&&" C* | ||
> C* @:compare=/{!?(?:<:|<>?=?|>=?|=)} C* | ||
> C* @ if=("if" t=expr ("else" f=expr)?)* C* | ||
> C* @:binop=/{\+\+|[+|]|-%?} C* | ||
> C* @:binop=/{&~|&|~~?|[-<][-&][->]} C* | ||
> C* @:binop=/{//|[*/%]|\\} C* | ||
> C* @:rbinop="^" C* | ||
> C* unop=/{:>|=>|>>|[-+!*^]}* @ C* | ||
> C* @:binop=">>>" C* | ||
> C* @ count="count"? C* touch? C* | ||
> C* (get | @) tail=( | ||
get | ||
| call=("(" | ||
arg=( | ||
expr (":" end=expr? (":" step=expr)?)? | ||
| ":" end=expr (":" step=expr)? | ||
):",", | ||
")") | ||
)* C* | ||
> C* "{" C* rel=(names tuple=("(" v=@:",", ")"):",",?) "}" C* | ||
| C* "{" C* set=(elt=@:",",?) "}" C* | ||
| C* "{" C* dict=((key=@ ":" value=@):",",?) "}" C* | ||
| C* cond=("cond" "(" (key=@ ":" value=@):",",? ("*" ":" f=expr)? ")") C* | ||
| C* cond=(("(" control_var=expr ")" | IDENT)? C* "cond" "(" (key=@ ":" value=@):",",? ("*" ":" f=expr)? ")") C* | ||
| C* "[" C* array=(item=@:",",?) "]" C* | ||
| C* "{:" C* embed=(grammar=@ ":" subgrammar=%%ast) ":}" C* | ||
| C* op="\\\\" @ C* | ||
| C* fn="\\" IDENT @ C* | ||
| C* "//" pkg=( "{" dot="."? PKGPATH "}" | std=IDENT?) | ||
| C* "(" tuple=(pairs=(name? ":" v=@):",",?) ")" C* | ||
| C* "(" @ ")" C* | ||
| C* let=("let" C* IDENT C* "=" C* @ %%bind C* ";" C* @) C* | ||
| C* xstr C* | ||
| C* IDENT C* | ||
| C* STR C* | ||
| C* NUM C*; | ||
nest -> C* "nest" names IDENT C*; | ||
unnest -> C* "unnest" IDENT C*; | ||
touch -> C* ("->*" ("&"? IDENT | STR))+ "(" expr:"," ","? ")" C*; | ||
get -> C* dot="." ("&"? IDENT | STR | "*") C*; | ||
names -> C* "|" C* IDENT:"," C* "|" C*; | ||
name -> C* IDENT C* | C* STR C*; | ||
xstr -> C* quote=/{\$"\s*} part=( sexpr | fragment=/{(?: \\. | \$[^{"] | [^\\"$] )+} )* '"' C* | ||
| C* quote=/{\$'\s*} part=( sexpr | fragment=/{(?: \\. | \$[^{'] | [^\\'$] )+} )* "'" C* | ||
| C* quote=/{\$‵\s*} part=( sexpr | fragment=/{(?: ‵‵ | \$[^{‵] | [^‵ $] )+} )* "‵" C*; | ||
sexpr -> "${" | ||
C* expr C* | ||
control=/{ (?: : [-+#*\.\_0-9a-z]* (?: : (?: \\. | [^\\:}] )* ){0,2} )? } | ||
close=/{\}\s*}; | ||
|
||
ARROW -> /{:>|=>|>>|orderby|order|where|sum|max|mean|median|min}; | ||
IDENT -> /{ \. | [$@A-Za-z_][0-9$@A-Za-z_]* }; | ||
PKGPATH -> /{ (?: \\ | [^\\}] )* }; | ||
STR -> /{ " (?: \\. | [^\\"] )* " | ||
| ' (?: \\. | [^\\'] )* ' | ||
| ‵ (?: ‵‵ | [^‵ ] )* ‵ | ||
}; | ||
NUM -> /{ (?: \d+(?:\.\d*)? | \.\d+ ) (?: [Ee][-+]?\d+ )? }; | ||
C -> /{ # .* $ }; | ||
|
||
.wrapRE -> /{\s*()\s*}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package syntax | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/arr-ai/wbnf/wbnf" | ||
) | ||
|
||
func unfakeBackquote(s string) string { | ||
return strings.ReplaceAll(s, "`", "`") | ||
} | ||
|
||
var arraiParsers = wbnf.MustCompile(unfakeBackquote(` | ||
expr -> C* amp="&"* @ C* arrow=( | ||
nest | | ||
unnest | | ||
ARROW @ | | ||
binding="->" C* "\\" C* IDENT C* %%bind C* @ | | ||
binding="->" C* %%bind @ | ||
)* C* | ||
> C* @:binop=("with" | "without") C* | ||
> C* @:binop="||" C* | ||
> C* @:binop="&&" C* | ||
> C* @:compare=/{!?(?:<:|<>?=?|>=?|=)} C* | ||
> C* @ if=("if" t=expr ("else" f=expr)?)* C* | ||
> C* @:binop=/{\+\+|[+|]|-%?} C* | ||
> C* @:binop=/{&~|&|~~?|[-<][-&][->]} C* | ||
> C* @:binop=/{//|[*/%]|\\} C* | ||
> C* @:rbinop="^" C* | ||
> C* unop=/{:>|=>|>>|[-+!*^]}* @ C* | ||
> C* @:binop=">>>" C* | ||
> C* @ count="count"? C* touch? C* | ||
> C* (get | @) tail=( | ||
get | ||
| call=("(" | ||
arg=( | ||
expr (":" end=expr? (":" step=expr)?)? | ||
| ":" end=expr (":" step=expr)? | ||
):",", | ||
")") | ||
)* C* | ||
> C* "{" C* rel=(names tuple=("(" v=@:",", ")"):",",?) "}" C* | ||
| C* "{" C* set=(elt=@:",",?) "}" C* | ||
| C* "{" C* dict=((key=@ ":" value=@):",",?) "}" C* | ||
| C* cond=("cond" "(" (key=@ ":" value=@):",",? ("*" ":" f=expr)? ")") C* | ||
| C* cond=(("(" control_var=expr ")" | IDENT)? C* "cond" "(" (key=@ ":" value=@):",",? ("*" ":" f=expr)? ")") C* | ||
| C* "[" C* array=(item=@:",",?) "]" C* | ||
| C* "{:" C* embed=(grammar=@ ":" subgrammar=%%ast) ":}" C* | ||
| C* op="\\\\" @ C* | ||
| C* fn="\\" IDENT @ C* | ||
| C* "//" pkg=( "{" dot="."? PKGPATH "}" | std=IDENT?) | ||
| C* "(" tuple=(pairs=(name? ":" v=@):",",?) ")" C* | ||
| C* "(" @ ")" C* | ||
| C* let=("let" C* IDENT C* "=" C* @ %%bind C* ";" C* @) C* | ||
| C* xstr C* | ||
| C* IDENT C* | ||
| C* STR C* | ||
| C* NUM C*; | ||
nest -> C* "nest" names IDENT C*; | ||
unnest -> C* "unnest" IDENT C*; | ||
touch -> C* ("->*" ("&"? IDENT | STR))+ "(" expr:"," ","? ")" C*; | ||
get -> C* dot="." ("&"? IDENT | STR | "*") C*; | ||
names -> C* "|" C* IDENT:"," C* "|" C*; | ||
name -> C* IDENT C* | C* STR C*; | ||
xstr -> C* quote=/{\$"\s*} part=( sexpr | fragment=/{(?: \\. | \$[^{"] | [^\\"$] )+} )* '"' C* | ||
| C* quote=/{\$'\s*} part=( sexpr | fragment=/{(?: \\. | \$[^{'] | [^\\'$] )+} )* "'" C* | ||
| C* quote=/{\$‵\s*} part=( sexpr | fragment=/{(?: ‵‵ | \$[^{‵] | [^‵ $] )+} )* "‵" C*; | ||
sexpr -> "${" | ||
C* expr C* | ||
control=/{ (?: : [-+#*\.\_0-9a-z]* (?: : (?: \\. | [^\\:}] )* ){0,2} )? } | ||
close=/{\}\s*}; | ||
ARROW -> /{:>|=>|>>|orderby|order|where|sum|max|mean|median|min}; | ||
IDENT -> /{ \. | [$@A-Za-z_][0-9$@A-Za-z_]* }; | ||
PKGPATH -> /{ (?: \\ | [^\\}] )* }; | ||
STR -> /{ " (?: \\. | [^\\"] )* " | ||
| ' (?: \\. | [^\\'] )* ' | ||
| ‵ (?: ‵‵ | [^‵ ] )* ‵ | ||
}; | ||
NUM -> /{ (?: \d+(?:\.\d*)? | \.\d+ ) (?: [Ee][-+]?\d+ )? }; | ||
C -> /{ # .* $ }; | ||
.wrapRE -> /{\s*()\s*}; | ||
`), nil) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
) | ||
|
||
// Reads ../syntax/arrai.wbnf file | ||
// and encodes them as strings literals in ../syntax/parser.go | ||
func main() { | ||
data, err := ioutil.ReadFile(os.Args[1]) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
content := append( | ||
[]byte("package syntax\n\nfunc unfakeBackquote(s string) string {\n return strings.ReplaceAll(s, \"`\", \"`\")\n}\n\nvar arraiParsers = wbnf.MustCompile(unfakeBackquote(`\n"), //nolint:lll | ||
data...) | ||
content = append(content, []byte("\n`), nil)")...) | ||
err = ioutil.WriteFile(os.Args[2], content, 0644) | ||
if err != nil { | ||
panic(err) | ||
} | ||
fmt.Println("arrai parser generated") | ||
} |