| 1 | //Copyright 2012 Vastech SA (PTY) LTD |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package examplescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/usercontext`/ |
| 16 | |
| 17 | import ( |
| 18 | "stringsscip-go gomod github.com/golang/go/src go1.24 strings/" |
| 19 | "testingscip-go gomod github.com/golang/go/src go1.24 testing/" |
| 20 | |
| 21 | "github.com/goccmack/gocc/example/usercontext/astscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/usercontext/ast`/" |
| 22 | "github.com/goccmack/gocc/example/usercontext/lexerscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/usercontext/lexer`/" |
| 23 | "github.com/goccmack/gocc/example/usercontext/parserscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/usercontext/parser`/" |
| 24 | ) |
| 25 | |
| 26 | func tryParsescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/usercontext`/tryParse().(t *testingscip-go gomod github.com/golang/go/src go1.24 testing/.Tscip-go gomod github.com/golang/go/src go1.24 testing/T#, code string, lexContext, parseContext interface{}) (interface{}, error) { |
| 27 | t.Helperscip-go gomod github.com/golang/go/src go1.24 testing/common#Helper().() |
| 28 | |
| 29 | l := lexerscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/usercontext/lexer`/.NewLexerscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/usercontext/lexer`/NewLexer().([]byte(code)) |
| 30 | l.Contextscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/usercontext/lexer`/Lexer#Context. = lexContext |
| 31 | |
| 32 | p := parserscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/usercontext/parser`/.NewParserscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/usercontext/parser`/NewParser().() |
| 33 | p.Contextscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/usercontext/parser`/Parser#Context. = parseContext |
| 34 | |
| 35 | return p.Parsescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/usercontext/parser`/Parser#Parse().(l) |
| 36 | } |
| 37 | |
| 38 | func TestTokenContextscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/usercontext`/TestTokenContext().(t *testingscip-go gomod github.com/golang/go/src go1.24 testing/.Tscip-go gomod github.com/golang/go/src go1.24 testing/T#) { |
| 39 | t.Runscip-go gomod github.com/golang/go/src go1.24 testing/T#Run().("nil context", func(t *testingscip-go gomod github.com/golang/go/src go1.24 testing/.Tscip-go gomod github.com/golang/go/src go1.24 testing/T#) { |
| 40 | t.Runscip-go gomod github.com/golang/go/src go1.24 testing/T#Run().("successful parse", func(t *testingscip-go gomod github.com/golang/go/src go1.24 testing/.Tscip-go gomod github.com/golang/go/src go1.24 testing/T#) { |
| 41 | if _, err := tryParsescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/usercontext`/tryParse().(t, "Life Universe Everything ...42...", nil, nil); err != nil { |
| 42 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24 testing/common#Fatalf().("not expecting an error, got: %q", err.Error()) |
| 43 | } |
| 44 | }) |
| 45 | |
| 46 | t.Runscip-go gomod github.com/golang/go/src go1.24 testing/T#Run().("syntax error", func(t *testingscip-go gomod github.com/golang/go/src go1.24 testing/.Tscip-go gomod github.com/golang/go/src go1.24 testing/T#) { |
| 47 | // missing identifiers. |
| 48 | _, err := tryParsescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/usercontext`/tryParse().(t, "...42...", nil, nil) |
| 49 | if err == nil { |
| 50 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24 testing/common#Fatalf().("expecting an error") |
| 51 | } |
| 52 | const expected = `1:1: error: expected either capitalized or lowercase; got: "...42..."` |
| 53 | if err.Error() != expected { |
| 54 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24 testing/common#Fatalf().("incorrect error:\nexpected: %q\nactual : %q", expected, err.Error()) |
| 55 | } |
| 56 | t.Logscip-go gomod github.com/golang/go/src go1.24 testing/common#Log().(err.Error()) |
| 57 | }) |
| 58 | |
| 59 | t.Runscip-go gomod github.com/golang/go/src go1.24 testing/T#Run().("context type mismatch", func(t *testingscip-go gomod github.com/golang/go/src go1.24 testing/.Tscip-go gomod github.com/golang/go/src go1.24 testing/T#) { |
| 60 | // if we use an identifier, it requires context, and the ast code will |
| 61 | // reject the context for being nil. |
| 62 | _, err := tryParsescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/usercontext`/tryParse().(t, "sixbyseven ...42...", nil, nil) |
| 63 | if err == nil { |
| 64 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24 testing/common#Fatalf().("expecting an error") |
| 65 | } |
| 66 | // not sure why err.ErrorToken has 12 in its pos.Column field, but currently |
| 67 | // this is correct. |
| 68 | const expected = `1:12: error: sixbyseven: NewIdentifier expected ParserContext, got <nil>` |
| 69 | if err.Error() != expected { |
| 70 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24 testing/common#Fatalf().("incorrect error:\nexpected: %q\nactual : %q", expected, err.Error()) |
| 71 | } |
| 72 | t.Logscip-go gomod github.com/golang/go/src go1.24 testing/common#Log().(err.Error()) |
| 73 | }) |
| 74 | }) |
| 75 | |
| 76 | t.Runscip-go gomod github.com/golang/go/src go1.24 testing/T#Run().("contexts", func(t *testingscip-go gomod github.com/golang/go/src go1.24 testing/.Tscip-go gomod github.com/golang/go/src go1.24 testing/T#) { |
| 77 | lc := &astscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/usercontext/ast`/.LexicalContextscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/usercontext/ast`/LexicalContext#{SourceFilescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/usercontext/ast`/LexicalContext#SourceFile.: "bender.txt", ForbiddenWordsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/usercontext/ast`/LexicalContext#ForbiddenWords.: []string{"daisy"}} |
| 78 | pc := &astscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/usercontext/ast`/.ParserContextscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/usercontext/ast`/ParserContext#{Visitorsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/usercontext/ast`/ParserContext#Visitors.: make([]string, 0)} // not using any other fields |
| 79 | |
| 80 | sym, err := tryParsescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/usercontext`/tryParse().(t, "Bite My shiny metal ...42...", lc, pc) |
| 81 | if err != nil { |
| 82 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24 testing/common#Fatalf().("not expecting an error, got: %s", err.Error()) |
| 83 | } |
| 84 | t.Logfscip-go gomod github.com/golang/go/src go1.24 testing/common#Logf().("returned symbol was %#+v\n", sym) |
| 85 | |
| 86 | // Because of the grammar, the ast should be just the last Identifier created by |
| 87 | // "Words", which should correspond to "metal". |
| 88 | switch ident := sym.(type) { |
| 89 | case *astscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/usercontext/ast`/.Identifierscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/usercontext/ast`/Identifier#: |
| 90 | if string(ident.Namescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/usercontext/ast`/Identifier#Name..Litscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/usercontext/token`/Token#Lit.) != "metal" { |
| 91 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24 testing/common#Fatalf().("expected 'metal' symbol, got: %s: %v", string(ident.Namescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/usercontext/ast`/Identifier#Name..Litscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/usercontext/token`/Token#Lit.), sym) |
| 92 | } |
| 93 | if ident.ParserContextscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/usercontext/ast`/Identifier#ParserContext. != pc { |
| 94 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24 testing/common#Fatalf().("expected ident.ParserContext to be &ParserContext, got: %v", ident.ParserContextscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/usercontext/ast`/Identifier#ParserContext.) |
| 95 | } |
| 96 | if ident.Namescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/usercontext/ast`/Identifier#Name..Posscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/usercontext/token`/Token#Pos..Contextscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/usercontext/token`/Pos#Context. != lc { |
| 97 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24 testing/common#Fatalf().("expected ident.Name.Pos.Context to be &LexicalContext, got: %v", ident.Namescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/usercontext/ast`/Identifier#Name..Posscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/usercontext/token`/Token#Pos..Contextscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/usercontext/token`/Pos#Context.) |
| 98 | } |
| 99 | |
| 100 | default: |
| 101 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24 testing/common#Fatalf().("first argument of parse is not the identifier we expected: got %#+v", sym) |
| 102 | } |
| 103 | |
| 104 | // Verify that ParserContext.Visitors was populated. |
| 105 | if len(pc.Visitorsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/usercontext/ast`/ParserContext#Visitors.) != 2 { |
| 106 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24 testing/common#Fatalf().("wrong count for pc.Visitors, expected: %d, got: %d", 2, len(pc.Visitorsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/usercontext/ast`/ParserContext#Visitors.)) |
| 107 | } |
| 108 | expected := "shiny metal" |
| 109 | actual := stringsscip-go gomod github.com/golang/go/src go1.24 strings/.Joinscip-go gomod github.com/golang/go/src go1.24 strings/Join().(pc.Visitorsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/usercontext/ast`/ParserContext#Visitors., " ") |
| 110 | t.Logfscip-go gomod github.com/golang/go/src go1.24 testing/common#Logf().("visitors: %s", actual) |
| 111 | if actual != expected { |
| 112 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24 testing/common#Fatalf().("wrong Visitor list, expected: %q, got: %q", expected, actual) |
| 113 | } |
| 114 | }) |
| 115 | } |