diff --git a/parser/fspl/README.md b/parser/fspl/README.md index 5ea9097..277e88e 100644 --- a/parser/fspl/README.md +++ b/parser/fspl/README.md @@ -70,6 +70,7 @@ expression, the parser follows this decision tree to determine what to parse: | | 'nil' =LiteralNil | | 'if' =IfElse | | 'match' =Match +| | 'switch' =Switch | | 'loop' =Loop | | +Colon =Declaration | | +DoubleColon =Call diff --git a/parser/fspl/expression.go b/parser/fspl/expression.go index decc3fa..3377030 100644 --- a/parser/fspl/expression.go +++ b/parser/fspl/expression.go @@ -580,18 +580,24 @@ func (this *treeParser) parseMatch () (*entity.Match, error) { if err != nil { return nil, err } for this.Is(lexer.Symbol) && this.ValueIs("|") { - cas, err := this.parseCase() + cas, err := this.parseMatchCase() if err != nil { return nil, err } match.Cases = append(match.Cases, cas) } + if this.Is(lexer.Star) { + defaul, err := this.parseDefaultCase() + if err != nil { return nil, err } + match.Default = defaul + } + return match, nil } -func (this *treeParser) parseCase () (*entity.Case, error) { +func (this *treeParser) parseMatchCase () (*entity.MatchCase, error) { err := this.ExpectValue(lexer.Symbol, "|") if err != nil { return nil, err } - cas := &entity.Case { + cas := &entity.MatchCase { Pos: this.Pos(), } @@ -604,6 +610,20 @@ func (this *treeParser) parseCase () (*entity.Case, error) { return cas, nil } +func (this *treeParser) parseDefaultCase () (*entity.DefaultCase, error) { + err := this.Expect(lexer.Star) + if err != nil { return nil, err } + cas := &entity.DefaultCase { + Pos: this.Pos(), + } + + this.Next() + cas.Expression, err = this.parseExpression() + if err != nil { return nil, err } + + return cas, nil +} + func (this *treeParser) parseLoop () (*entity.Loop, error) { err := this.ExpectValue(lexer.Ident, "loop") if err != nil { return nil, err } diff --git a/parser/fspl/parser_test.go b/parser/fspl/parser_test.go index ab71660..406ee93 100644 --- a/parser/fspl/parser_test.go +++ b/parser/fspl/parser_test.go @@ -74,7 +74,7 @@ testString (test, - [loop]:Int = {i:Int=0 if [< i 3] then return 1 loop {[print 3] if [> i 3] then break 0 i=[++ i]}} - [for s:String] = for i:Index e:Byte in str if [> i 3] then [break e] - [for s:String] = for e:Byte in s [break e] -- [matchToInt u:(| Int F64)]:Int = match u | u:Int u | u:F64 [~ Int u]`, +- [matchToInt u:(| Int F64)]:Int = match u | u:Int u | u:F64 [~ Int u] * 0`, // input ` [var] = sdfdf @@ -127,6 +127,7 @@ testString (test, [matchToInt u:(| Int F64)]:Int = match u | u:Int u | u:F64 [~Int u] + * 0 `) }