| 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 | "fmt" |
| 19 | "testing" |
| 20 | |
| 21 | "github.com/goccmack/gocc/internal/lexer/symbols" |
| 22 | ) |
| 23 | |
| 24 | /* |
| 25 | id : _a | _b _a => |
| 26 | |
| 27 | id : • _a | _b _a |
| 28 | id : _a | • _b _a |
| 29 | |
| 30 | id : • a | _b => |
| 31 | |
| 32 | id : (_a | _b _a) • |
| 33 | |
| 34 | id : a | • _b _a => |
| 35 | |
| 36 | id : _a | _b • _a => |
| 37 | id : (_a | _b _A) • |
| 38 | */ |
| 39 | func Test1(t *testing.T) { |
| 40 | src := `_a : 'a'; _b : 'b' ; id : _a | _b _a ;` |
| 41 | g := parse(src, t) |
| 42 | symbols := symbols.NewSymbols(g.LexPart) |
| 43 | |
| 44 | items1 := NewItem("id", g.LexPart, symbols).Emoves() |
| 45 | exp1 := "id : • _a | _b _a" |
| 46 | exp2 := "id : _a | • _b _a" |
| 47 | matchItems(t, items1, exp1, exp2) |
| 48 | |
| 49 | exp3 := "id : (_a | _b _a) •" |
| 50 | matchItems(t, findItem(exp1, items1).MoveRegDefId("_a"), exp3) |
| 51 | |
| 52 | exp4 := "id : _a | _b • _a" |
| 53 | matchItems(t, findItem(exp2, items1).MoveRegDefId("_b"), exp4) |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | /* |
| 58 | id : _a {_b} => |
| 59 | |
| 60 | id : • _a {_b} => |
| 61 | id : _a {• _b} |
| 62 | id : _a {_b} • |
| 63 | */ |
| 64 | func Test2(t *testing.T) { |
| 65 | src := `_a : 'a'; _b : 'b' ; id : _a {_b};` |
| 66 | g := parse(src, t) |
| 67 | symbols := symbols.NewSymbols(g.LexPart) |
| 68 | |
| 69 | items1 := NewItem("id", g.LexPart, symbols).Emoves() |
| 70 | |
| 71 | exp1 := "id : • _a {_b}" |
| 72 | matchItems(t, items1, exp1) |
| 73 | |
| 74 | items2 := findItem(exp1, items1).MoveRegDefId("_a") |
| 75 | exp2 := "id : _a {• _b}" |
| 76 | exp3 := "id : _a {_b} •" |
| 77 | matchItems(t, items2, exp2, exp3) |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | id : [.] . => |
| 82 | |
| 83 | id : [• .] . |
| 84 | id : [.] • . |
| 85 | |
| 86 | id : [.] • . => |
| 87 | |
| 88 | id : [.] . • |
| 89 | */ |
| 90 | func Test3(t *testing.T) { |
| 91 | src := `id : [.] .;` |
| 92 | g := parse(src, t) |
| 93 | symbols := symbols.NewSymbols(g.LexPart) |
| 94 | |
| 95 | items := NewItem("id", g.LexPart, symbols).Emoves() |
| 96 | |
| 97 | exp1 := "id : [• .] ." |
| 98 | exp2 := "id : [.] • ." |
| 99 | matchItems(t, items, exp1, exp2) |
| 100 | exp3 := "id : [.] . •" |
| 101 | matchItems(t, findItem(exp2, items).MoveDot(), exp3) |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | id : _a (_a | _b) _c => |
| 106 | |
| 107 | id : •a (_a | _b) _c => |
| 108 | id : _a (• _a | _b) _c |
| 109 | id : _a (_a | • _b) _c |
| 110 | |
| 111 | id : _a (• _a | _b) _c => |
| 112 | |
| 113 | id : _a (_a | _b) •_c => |
| 114 | id : _a (_a | _b) _c • |
| 115 | |
| 116 | id : _a (_a | • _b) _c |
| 117 | |
| 118 | id : _a (_a | _b) •_c => |
| 119 | id : _a (_a | _b) _c • |
| 120 | */ |
| 121 | func Test4(t *testing.T) { |
| 122 | src := "id : _a (_a | _b) _c ;" |
| 123 | g := parse(src, t) |
| 124 | symbols := symbols.NewSymbols(g.LexPart) |
| 125 | |
| 126 | items := NewItem("id", g.LexPart, symbols).Emoves() |
| 127 | |
| 128 | exp1 := "id : • _a (_a | _b) _c" |
| 129 | matchItems(t, items, exp1) |
| 130 | |
| 131 | items1 := findItem(exp1, items).MoveRegDefId("_a") |
| 132 | exp2 := "id : _a (• _a | _b) _c" |
| 133 | exp3 := "id : _a (_a | • _b) _c" |
| 134 | matchItems(t, items1, exp2, exp3) |
| 135 | |
| 136 | items2 := findItem(exp2, items1).MoveRegDefId("_a") |
| 137 | exp4 := "id : _a (_a | _b) • _c" |
| 138 | matchItems(t, items2, exp4) |
| 139 | items3 := findItem(exp4, items2).MoveRegDefId("_c") |
| 140 | exp5 := "id : _a (_a | _b) _c •" |
| 141 | matchItems(t, items3, exp5) |
| 142 | |
| 143 | items4 := findItem(exp3, items1).MoveRegDefId("_b") |
| 144 | exp6 := "id : _a (_a | _b) • _c" |
| 145 | matchItems(t, items4, exp6) |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | id : {_a | _b} => |
| 150 | |
| 151 | id : {• _a | _b} |
| 152 | id : {_a | • _b} |
| 153 | id : {_a | _b} • |
| 154 | |
| 155 | id : {• _a | _b} => |
| 156 | |
| 157 | id : {• _a | _b} |
| 158 | id : {_a | • _b} |
| 159 | id : {_a | _b} • |
| 160 | */ |
| 161 | func Test5(t *testing.T) { |
| 162 | src := `id : {_a | _b};` |
| 163 | g := parse(src, t) |
| 164 | symbols := symbols.NewSymbols(g.LexPart) |
| 165 | |
| 166 | items := NewItem("id", g.LexPart, symbols).Emoves() |
| 167 | |
| 168 | exp1, exp2, exp3 := "id : {• _a | _b}", "id : {_a | • _b}", "id : {_a | _b} •" |
| 169 | matchItems(t, items, exp1, exp2, exp3) |
| 170 | |
| 171 | items1 := findItem(exp1, items).MoveRegDefId("_a") |
| 172 | matchItems(t, items1, exp1, exp2, exp3) |
| 173 | |
| 174 | items2 := findItem(exp2, items).MoveRegDefId("_b") |
| 175 | matchItems(t, items2, exp1, exp2, exp3) |
| 176 | } |
| 177 | |
| 178 | func findItem(item string, items []*Item) *Item { |
| 179 | for _, item1 := range items { |
| 180 | if item1.String() == item { |
| 181 | return item1 |
| 182 | } |
| 183 | } |
| 184 | return nil |
| 185 | } |
| 186 | |
| 187 | func findItemWithPosLevel(items []*Item, level int) *Item { |
| 188 | for _, item := range items { |
| 189 | if item.pos.level() == level { |
| 190 | return item |
| 191 | } |
| 192 | } |
| 193 | return nil |
| 194 | } |
| 195 | |
| 196 | func matchItems(t *testing.T, generatedItems []*Item, expectedItems ...string) { |
| 197 | dumpGeneratedItems := false |
| 198 | if len(generatedItems) != len(expectedItems) { |
| 199 | dumpGeneratedItems = true |
| 200 | t.Errorf("Expected %d items, got %d\n", len(expectedItems), len(generatedItems)) |
| 201 | } |
| 202 | itemMap := make(map[string]bool) |
| 203 | for _, item := range generatedItems { |
| 204 | itemMap[item.String()] = true |
| 205 | } |
| 206 | |
| 207 | for _, item := range expectedItems { |
| 208 | if _, ok := itemMap[item]; !ok { |
| 209 | dumpGeneratedItems = true |
| 210 | t.Errorf("Expected item >%s< not found\n", item) |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | if dumpGeneratedItems { |
| 215 | fmt.Printf("item_test.matchItems: Generated items:\n") |
| 216 | for i, item := range generatedItems { |
| 217 | fmt.Printf("\t%d: %s\n", i, item) |
| 218 | // fmt.Printf("%s", item.pos) |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | // func parse(src string, t *testing.T) *ast.Grammar { |
| 224 | // scanner := new(scanner.Scanner) |
| 225 | // scanner.Init([]byte(src), token.FRONTENDTokens) |
| 226 | // parser := parser.NewParser(parser.ActionTable, parser.GotoTable, parser.ProductionsTable, token.FRONTENDTokens) |
| 227 | // g, err := parser.Parse(scanner) |
| 228 | // if err != nil { |
| 229 | // t.Fatal(fmt.Sprintf("Parse error: %s\n", err)) |
| 230 | // return nil |
| 231 | // } |
| 232 | |
| 233 | // return g.(*ast.Grammar) |
| 234 | // } |