]> gitweb.factorcode.org Git - factor.git/blob - extra/infix/parser/parser.factor
factor: trim more using lists.
[factor.git] / extra / infix / parser / parser.factor
1 ! Copyright (C) 2009 Philipp Brüschweiler
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: infix.ast infix.tokenizer kernel multiline peg.ebnf
4 sequences strings vectors ;
5 IN: infix.parser
6
7 EBNF: parse-infix [=[
8 Number      = . ?[ ast-value? ]?
9 Identifier  = . ?[ string? ]?
10 Array       = Identifier:i "[" Sum:s "]" => [[ i s ast-array boa ]]
11 Slice1      = Identifier:i "[" Sum?:from ":" Sum?:to "]" => [[ i from to f ast-slice boa ]]
12 Slice2      = Identifier:i "[" Sum?:from ":" Sum?:to ":" Sum?:step "]" => [[ i from to step ast-slice boa ]]
13 Slice       = Slice1 | Slice2
14 Function    = Identifier:i "(" FunArgs?:a ")" => [[ i a [ V{ } ] unless* ast-function boa ]]
15
16 FunArgs     =   FunArgs:a "," Sum:s => [[ s a push a ]]
17               | Sum:s => [[ s 1vector ]]
18
19 Terminal    =   ("-"|"+"):op Terminal:term => [[ term op "-" = [ ast-negation boa ] when ]]
20               | "(" Sum:s ")" => [[ s ]]
21               | Number | Array | Slice | Function
22               | Identifier => [[ ast-local boa ]]
23
24 Product     =   Product:p ("**"|"*"|"/"|"%"):op Terminal:term  => [[ p term op ast-op boa ]]
25               | Terminal
26
27 Sum         =   Sum:s ("+"|"-"):op Product:p  => [[ s p op ast-op boa ]]
28               | Product
29
30 End         = !(.)
31 Expression  = Sum End
32 ]=]
33
34 : build-infix-ast ( string -- ast )
35     tokenize-infix parse-infix ;