1+ using System ;
2+ using System . Collections . Immutable ;
3+ using Pidgin ;
4+ using Pidgin . Expression ;
5+
6+ using System ;
7+ using System . Collections . Immutable ;
8+ using Pidgin ;
9+ using Pidgin . Expression ;
10+ using static Pidgin . Parser ;
11+ using static Pidgin . Parser < char > ;
12+
13+ namespace SimpleStateMachine . StructuralSearch . Sandbox
14+ {
15+ public static class ExprParser
16+ {
17+ private static Parser < char , T > Tok < T > ( Parser < char , T > token )
18+ => Try ( token ) . Before ( SkipWhitespaces ) ;
19+ private static Parser < char , string > Tok ( string token )
20+ => Tok ( String ( token ) ) ;
21+
22+ private static Parser < char , T > Parenthesised < T > ( Parser < char , T > parser )
23+ => parser . Between ( Tok ( "(" ) , Tok ( ")" ) ) ;
24+
25+ private static Parser < char , Func < IExpr , IExpr , IExpr > > Binary ( Parser < char , BinaryOperatorType > op )
26+ => op . Select < Func < IExpr , IExpr , IExpr > > ( type => ( l , r ) => new BinaryOp ( type , l , r ) ) ;
27+ private static Parser < char , Func < IExpr , IExpr > > Unary ( Parser < char , UnaryOperatorType > op )
28+ => op . Select < Func < IExpr , IExpr > > ( type => o => new UnaryOp ( type , o ) ) ;
29+
30+ private static readonly Parser < char , Func < IExpr , IExpr , IExpr > > Add
31+ = Binary ( Tok ( "+" ) . ThenReturn ( BinaryOperatorType . Add ) ) ;
32+ private static readonly Parser < char , Func < IExpr , IExpr , IExpr > > Sub
33+ = Binary ( Tok ( "-" ) . ThenReturn ( BinaryOperatorType . Sub ) ) ;
34+ private static readonly Parser < char , Func < IExpr , IExpr , IExpr > > Div
35+ = Binary ( Tok ( "/" ) . ThenReturn ( BinaryOperatorType . Div ) ) ;
36+ private static readonly Parser < char , Func < IExpr , IExpr , IExpr > > Mul
37+ = Binary ( Tok ( "*" ) . ThenReturn ( BinaryOperatorType . Mul ) ) ;
38+ private static readonly Parser < char , Func < IExpr , IExpr > > Decrement
39+ = Unary ( Tok ( "--" ) . ThenReturn ( UnaryOperatorType . Decrement ) ) ;
40+ private static readonly Parser < char , Func < IExpr , IExpr > > Increment
41+ = Unary ( Tok ( "++" ) . ThenReturn ( UnaryOperatorType . Increment ) ) ;
42+
43+ private static readonly Parser < char , Func < IExpr , IExpr > > Minus
44+ = Unary ( Tok ( "-" ) . ThenReturn ( UnaryOperatorType . Minus ) ) ;
45+ private static readonly Parser < char , Func < IExpr , IExpr > > Plus
46+ = Unary ( Tok ( "+" ) . ThenReturn ( UnaryOperatorType . Plus ) ) ;
47+
48+ private static readonly Parser < char , IExpr > Identifier
49+ = Tok ( Letter . Then ( LetterOrDigit . ManyString ( ) , ( h , t ) => h + t ) )
50+ . Select < IExpr > ( name => new Identifier ( name ) )
51+ . Labelled ( "identifier" ) ;
52+
53+ private static readonly Parser < char , IExpr > Literal
54+ = Tok ( LongNum )
55+ . Select < IExpr > ( value => new Literal ( value ) )
56+ . Labelled ( "integer literal" ) ;
57+
58+ private static readonly Parser < char , IExpr > Expr = ExpressionParser . Build < char , IExpr > (
59+ expr => (
60+ OneOf (
61+ Identifier ,
62+ Literal ,
63+ Parenthesised ( expr ) . Labelled ( "parenthesised expression" )
64+ ) ,
65+ new [ ]
66+ {
67+ Operator . Prefix ( Decrement )
68+ . And ( Operator . Prefix ( Increment ) )
69+ . And ( Operator . Prefix ( Minus ) )
70+ . And ( Operator . Prefix ( Plus ) ) ,
71+ Operator . InfixL ( Mul ) ,
72+ Operator . InfixL ( Div ) ,
73+ Operator . InfixL ( Add ) ,
74+ Operator . InfixL ( Sub )
75+ }
76+ )
77+ ) . Labelled ( "expression" ) ;
78+
79+ public static IExpr ParseOrThrow ( string input )
80+ => Expr . ParseOrThrow ( input ) ;
81+
82+ }
83+ }
0 commit comments