Debian Codegraph @

internal/lexer/items/testutils_test.go

raw symbols SCIP
1 //Copyright 2013 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 items
16
17 import (
18 "testing"
19
20 "github.com/goccmack/gocc/internal/ast"
21 "github.com/goccmack/gocc/internal/frontend/parser"
22 "github.com/goccmack/gocc/internal/frontend/scanner"
23 "github.com/goccmack/gocc/internal/frontend/token"
24 )
25
26 func findSet(sets *ItemSets, items []string) *ItemSet {
27 for _, set := range sets.List() {
28 if setEqualItems(set, items) {
29 return set
30 }
31 }
32 return nil
33 }
34
35 func setEqualItems(set *ItemSet, items []string) bool {
36 if set.Size() != len(items) {
37 return false
38 }
39 for _, item := range items {
40 if !setContainItem(set, item) {
41 return false
42 }
43 }
44 return true
45 }
46
47 func setContainItem(set *ItemSet, item string) bool {
48 for _, setItem := range set.Items {
49 if setItem.str == item {
50 return true
51 }
52 }
53 return false
54 }
55
56 func parse(src string, t *testing.T) *ast.Grammar {
57 scanner := new(scanner.Scanner)
58 scanner.Init([]byte(src), token.FRONTENDTokens)
59 parser := parser.NewParser(parser.ActionTable, parser.GotoTable, parser.ProductionsTable, token.FRONTENDTokens)
60 g, err := parser.Parse(scanner)
61 if err != nil {
62 t.Fatalf("Parse error: %s\n", err)
63 return nil
64 }
65
66 return g.(*ast.Grammar)
67 }