1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
|
package participle
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestIssue3Example1(t *testing.T) {
type LAT1Decl struct {
SourceFilename string ` "source_filename" "=" @String`
DataLayout string `| "target" "datalayout" "=" @String`
TargetTriple string `| "target" "triple" "=" @String`
}
type LAT1Module struct {
Decls []*LAT1Decl `{ @@ }`
}
g := &LAT1Module{}
p := mustTestParser(t, g, UseLookahead(5))
err := p.ParseString(`
source_filename = "foo.c"
target datalayout = "bar"
target triple = "baz"
`, g)
require.NoError(t, err)
require.Equal(t,
g,
&LAT1Module{
Decls: []*LAT1Decl{
{SourceFilename: "foo.c"},
{DataLayout: "bar"},
{TargetTriple: "baz"},
},
})
}
type LAT2Config struct {
Entries []*LAT2Entry `@@ { @@ }`
}
type LAT2Entry struct {
Attribute *LAT2Attribute `@@`
Group *LAT2Group `| @@`
}
type LAT2Attribute struct {
Key string `@Ident "="`
Value string `@String`
}
type LAT2Group struct {
Name string `@Ident "{"`
Entries []*LAT2Entry `@@ { @@ } "}"`
}
func TestIssue3Example2(t *testing.T) {
g := &LAT2Config{}
p := mustTestParser(t, g, UseLookahead(2))
err := p.ParseString(`
key = "value"
block {
inner_key = "inner_value"
}
`, g)
require.NoError(t, err)
require.Equal(t,
g,
&LAT2Config{
Entries: []*LAT2Entry{
{Attribute: &LAT2Attribute{Key: "key", Value: "value"}},
{
Group: &LAT2Group{
Name: "block",
Entries: []*LAT2Entry{
{Attribute: &LAT2Attribute{Key: "inner_key", Value: "inner_value"}},
},
},
},
},
},
)
}
type LAT3Grammar struct {
Expenses []*LAT3Expense `{ @@ }`
}
type LAT3Expense struct {
Name string `@Ident "paid"`
Amount *LAT3Value `@@ { Ident } "."`
}
type LAT3Value struct {
Float float64 ` "$" @Float`
Integer int `| "$" @Int`
}
func TestIssue11(t *testing.T) {
g := &LAT3Grammar{}
p := mustTestParser(t, g, UseLookahead(5))
err := p.ParseString(`
A paid $30.80 for snacks.
B paid $70 for housecleaning.
C paid $63.50 for utilities.
`, g)
require.NoError(t, err)
require.Equal(t,
g,
&LAT3Grammar{
Expenses: []*LAT3Expense{
{Name: "A", Amount: &LAT3Value{Float: 30.8}},
{Name: "B", Amount: &LAT3Value{Integer: 70}},
{Name: "C", Amount: &LAT3Value{Float: 63.5}},
},
},
)
}
func TestLookaheadOptional(t *testing.T) {
type grammar struct {
Key string `[ @Ident "=" ]`
Value string `@Ident`
}
p := mustTestParser(t, &grammar{}, UseLookahead(5))
actual := &grammar{}
err := p.ParseString(`value`, actual)
require.NoError(t, err)
require.Equal(t, &grammar{Value: "value"}, actual)
err = p.ParseString(`key = value`, actual)
require.NoError(t, err)
require.Equal(t, &grammar{Key: "key", Value: "value"}, actual)
}
func TestLookaheadOptionalNoTail(t *testing.T) {
type grammar struct {
Key string `@Ident`
Value string `[ "=" @Int ]`
}
p := mustTestParser(t, &grammar{}, UseLookahead(5))
actual := &grammar{}
err := p.ParseString(`key`, actual)
require.NoError(t, err)
}
func TestLookaheadDisjunction(t *testing.T) {
type grammar struct {
B string ` "hello" @Ident "world"`
C string `| "hello" "world" @Ident`
A string `| "hello" @Ident`
}
p := mustTestParser(t, &grammar{}, UseLookahead(5))
g := &grammar{}
err := p.ParseString(`hello moo`, g)
require.NoError(t, err)
require.Equal(t, &grammar{A: "moo"}, g)
err = p.ParseString(`hello moo world`, g)
require.NoError(t, err)
require.Equal(t, &grammar{B: "moo"}, g)
}
func TestLookaheadNestedDisjunctions(t *testing.T) {
g := &struct {
A string ` "hello" ( "foo" @Ident | "bar" "waz" @Ident)`
B string `| "hello" @"world"`
}{}
p := mustTestParser(t, g, UseLookahead(5))
err := p.ParseString(`hello foo FOO`, g)
require.NoError(t, err)
require.Equal(t, g.A, "FOO")
err = p.ParseString(`hello world`, g)
require.NoError(t, err)
require.Equal(t, g.B, "world")
}
func TestLookaheadTerm(t *testing.T) {
g := &struct {
A string ` @Ident`
B struct {
A string `@String`
} `| @@`
C struct {
A string `@String`
B string `"…" @String`
} `| @@`
D struct {
A string `"[" @Ident "]"`
} `| @@`
E struct {
A string `"(" @Ident ")"`
} `| @@`
}{}
mustTestParser(t, g, UseLookahead(5))
}
// Term holds the different possible terms
type issue28Term struct {
KV *issue28KV ` @@ `
Text *string `| @String `
}
// KV represents a json kv
type issue28KV struct {
Key *issue28Key `@@`
Value *issue28Value `@@`
}
// Key holds the possible key types for a kv
type issue28Key struct {
Ident *string `@Ident ":"`
Str *string `| @String ":"`
}
// Value holds the possible values for a kv
type issue28Value struct {
Bool *bool `(@"true" | "false")`
Str *string `| @String`
Ident *string `| @Ident`
Int *int64 `| @Int`
Float *float64 `| @Float`
}
func TestIssue28(t *testing.T) {
p := mustTestParser(t, &issue28Term{}, UseLookahead(5))
actual := &issue28Term{}
err := p.ParseString(`"key": "value"`, actual)
require.NoError(t, err)
key := "key"
value := "value"
expected := &issue28Term{
KV: &issue28KV{
Key: &issue28Key{
Str: &key,
},
Value: &issue28Value{
Str: &value,
},
},
}
require.Equal(t, expected, actual)
err = p.ParseString(`"some text string"`, actual)
require.NoError(t, err)
text := "some text string"
expected = &issue28Term{
Text: &text,
}
require.Equal(t, expected, actual)
}
// This test used to fail because the lookahead table only tracks (root, depth, token) for each root. In this case there
// are two roots that have the same second token (0, 1, "=") and (2, 1, "="). As (depth, token) is the uniqueness
// constraint, this never disambiguates.
//
// To solve this, each ambiguous group will need to track the history of tokens.
//
// eg.
//
// 0. groups = [
// {history: [">"] roots: [0, 1]},
// {history: ["<"], roots: [2, 3]},
// ]
// 1. groups = [
// {history: [">", "="], roots: [0]},
// {history: [">"], roots: [1]},
// {history: ["<", "="], roots: [2]},
// {history: ["<"], roots: [3]},
// ]
func TestLookaheadWithConvergingTokens(t *testing.T) {
type grammar struct {
Left string `@Ident`
Op string `[ @( ">" "=" | ">" | "<" "=" | "<" )`
Next *grammar ` @@ ]`
}
p := mustTestParser(t, &grammar{}, UseLookahead(5))
actual := &grammar{}
err := p.ParseString("a >= b", actual)
require.NoError(t, err)
}
// type leftRecursionType struct {
// Type string ` @("int" | "float" | "string")`
// Function *leftRecursionFuncType `| @@`
// }
// type leftRecursionFuncType struct {
// Return *leftRecursionType `@@`
// Function string `@Ident`
// Args []*leftRecursionType `"(" @@ { "," @@ } ")"`
// }
// func TestLeftRecursion(t *testing.T) {
// p := mustTestParser(t, &leftRecursionType{}, UseLookahead(5))
// actual := &leftRecursionType{}
// err := p.ParseString(`int f()`, actual)
// require.NoError(t, err)
// require.Equal(t, &leftRecursionType{
// Function: &leftRecursionFuncType{
// Return: &leftRecursionType{Type: "int"},
// Function: "f",
// },
// }, actual)
// }
func TestIssue27(t *testing.T) {
type grammar struct {
Number int ` @(["-"] Int)`
String string `| @String`
}
p := mustTestParser(t, &grammar{})
actual := &grammar{}
err := p.ParseString(`- 100`, actual)
require.NoError(t, err)
require.Equal(t, &grammar{Number: -100}, actual)
err = p.ParseString(`100`, actual)
require.NoError(t, err)
require.Equal(t, &grammar{Number: 100}, actual)
}
func TestLookaheadDisambiguateByType(t *testing.T) {
type grammar struct {
Int int ` @(["-"] Int)`
Float float64 `| @(["-"] Float)`
}
p := mustTestParser(t, &grammar{}, UseLookahead(5))
actual := &grammar{}
err := p.ParseString(`- 100`, actual)
require.NoError(t, err)
require.Equal(t, &grammar{Int: -100}, actual)
err = p.ParseString(`- 100.5`, actual)
require.NoError(t, err)
require.Equal(t, &grammar{Float: -100.5}, actual)
}
func TestShowNearestError(t *testing.T) {
type grammar struct {
A string ` @"a" @"b" @"c"`
B string `| @"a" @"z"`
}
p := mustTestParser(t, &grammar{}, UseLookahead(10))
actual := &grammar{}
err := p.ParseString(`a b d`, actual)
require.EqualError(t, err, `1:5: unexpected token "d" (expected "c")`)
}
func TestRewindDisjunction(t *testing.T) {
type grammar struct {
Function string ` @Ident "(" ")"`
Ident string `| @Ident`
}
p := mustTestParser(t, &grammar{}, UseLookahead(2))
ast := &grammar{}
err := p.ParseString(`name`, ast)
require.NoError(t, err)
require.Equal(t, &grammar{Ident: "name"}, ast)
}
func TestRewindOptional(t *testing.T) {
type grammar struct {
Var string ` [ "int" "int" ] @Ident`
}
p := mustTestParser(t, &grammar{}, UseLookahead(3))
ast := &grammar{}
err := p.ParseString(`one`, ast)
require.NoError(t, err)
require.Equal(t, &grammar{Var: "one"}, ast)
err = p.ParseString(`int int one`, ast)
require.NoError(t, err)
require.Equal(t, &grammar{Var: "one"}, ast)
}
func TestRewindRepetition(t *testing.T) {
type grammar struct {
Ints []string `{ @"int" }`
Ident string `@Ident`
}
p := mustTestParser(t, &grammar{}, UseLookahead(3))
ast := &grammar{}
err := p.ParseString(`int int one`, ast)
require.NoError(t, err)
require.Equal(t, &grammar{Ints: []string{"int", "int"}, Ident: "one"}, ast)
err = p.ParseString(`int int one`, ast)
require.NoError(t, err)
require.Equal(t, &grammar{Ints: []string{"int", "int"}, Ident: "one"}, ast)
}
|