diff options
| author | Sérgio Queiroz <sqmedeiros@gmail.com> | 2017-12-19 13:13:10 -0300 |
|---|---|---|
| committer | Sérgio Queiroz <sqmedeiros@gmail.com> | 2017-12-19 13:13:10 -0300 |
| commit | c5574a9e8396498769e1f2a91609d08cbb0f86eb (patch) | |
| tree | 2c6e109132bc1f0b20a619dedd913cd20b18035b | |
| parent | c6b98103f0ab2c4afb7216e73f2eacac58cbf952 (diff) | |
| download | lpeglabel-c5574a9e8396498769e1f2a91609d08cbb0f86eb.tar.gz lpeglabel-c5574a9e8396498769e1f2a91609d08cbb0f86eb.tar.bz2 lpeglabel-c5574a9e8396498769e1f2a91609d08cbb0f86eb.zip | |
Updating relabel's parser after removing /{} and //{}
| -rw-r--r-- | relabel.lua | 38 | ||||
| -rw-r--r-- | testrelabelparser.lua | 41 |
2 files changed, 14 insertions, 65 deletions
diff --git a/relabel.lua b/relabel.lua index 6a676b9..3188163 100644 --- a/relabel.lua +++ b/relabel.lua | |||
| @@ -31,7 +31,7 @@ local errinfo = { | |||
| 31 | {"NoPatt", "no pattern found"}, | 31 | {"NoPatt", "no pattern found"}, |
| 32 | {"ExtraChars", "unexpected characters after the pattern"}, | 32 | {"ExtraChars", "unexpected characters after the pattern"}, |
| 33 | 33 | ||
| 34 | {"ExpPatt1", "expected a pattern after '/' and '//{...}', or the label(s) after '/{' and '//{'"}, | 34 | {"ExpPatt1", "expected a pattern after '/'"}, |
| 35 | 35 | ||
| 36 | {"ExpPatt2", "expected a pattern after '&'"}, | 36 | {"ExpPatt2", "expected a pattern after '&'"}, |
| 37 | {"ExpPatt3", "expected a pattern after '!'"}, | 37 | {"ExpPatt3", "expected a pattern after '!'"}, |
| @@ -52,8 +52,7 @@ local errinfo = { | |||
| 52 | {"ExpName2", "expected the name of a rule after '=' (no space)"}, | 52 | {"ExpName2", "expected the name of a rule after '=' (no space)"}, |
| 53 | {"ExpName3", "expected the name of a rule after '<' (no space)"}, | 53 | {"ExpName3", "expected the name of a rule after '<' (no space)"}, |
| 54 | 54 | ||
| 55 | {"ExpLab1", "expected at least one label after '{'"}, | 55 | {"ExpLab1", "expected a label after '{'"}, |
| 56 | {"ExpLab2", "expected a label after the comma"}, | ||
| 57 | 56 | ||
| 58 | {"ExpNameOrLab", "expected a name or label after '%' (no space)"}, | 57 | {"ExpNameOrLab", "expected a name or label after '%' (no space)"}, |
| 59 | 58 | ||
| @@ -216,41 +215,10 @@ local function NT (n, b) | |||
| 216 | end | 215 | end |
| 217 | end | 216 | end |
| 218 | 217 | ||
| 219 | local function choicerec (...) | ||
| 220 | local t = { ... } | ||
| 221 | local n = #t | ||
| 222 | local p = t[1] | ||
| 223 | local i = 2 | ||
| 224 | |||
| 225 | while i + 2 <= n do | ||
| 226 | -- t[i] is '/' or '//' | ||
| 227 | -- t[i+1] == false when there are no labels | ||
| 228 | -- t[i+2] is a pattern | ||
| 229 | if t[i] == '/' then | ||
| 230 | if not t[i+1] then | ||
| 231 | p = mt.__add(p, t[i+2]) | ||
| 232 | else | ||
| 233 | p = mm.Lc(p, t[i+2], unpack(t[i+1])) | ||
| 234 | end | ||
| 235 | else -- t[i] is '//' | ||
| 236 | p = mm.Rec(p, t[i+2], unpack(t[i+1])) | ||
| 237 | end | ||
| 238 | i = i + 3 | ||
| 239 | end | ||
| 240 | |||
| 241 | return p | ||
| 242 | end | ||
| 243 | |||
| 244 | 218 | ||
| 245 | local exp = m.P{ "Exp", | 219 | local exp = m.P{ "Exp", |
| 246 | Exp = S * ( m.V"Grammar" | 220 | Exp = S * ( m.V"Grammar" |
| 247 | + (m.V"Seq" * (S * ((m.C(m.P"//" + "/") * m.Ct(m.V"Labels")) + (m.C"/" * m.Cc(false))) | 221 | + m.Cf(m.V"Seq" * (S * "/" * expect(S * m.V"Seq", "ExpPatt1"))^0, mt.__add) ); |
| 248 | * expect(S * m.V"Seq", "ExpPatt1") | ||
| 249 | )^0 | ||
| 250 | ) / choicerec); | ||
| 251 | Labels = m.P"{" * expect(S * m.V"Label", "ExpLab1") | ||
| 252 | * (S * "," * expect(S * m.V"Label", "ExpLab2"))^0 | ||
| 253 | * expect(S * "}", "MisClose7"); | ||
| 254 | Seq = m.Cf(m.Cc(m.P"") * m.V"Prefix" * (S * m.V"Prefix")^0, mt.__mul); | 222 | Seq = m.Cf(m.Cc(m.P"") * m.V"Prefix" * (S * m.V"Prefix")^0, mt.__mul); |
| 255 | Prefix = "&" * expect(S * m.V"Prefix", "ExpPatt2") / mt.__len | 223 | Prefix = "&" * expect(S * m.V"Prefix", "ExpPatt2") / mt.__len |
| 256 | + "!" * expect(S * m.V"Prefix", "ExpPatt3") / mt.__unm | 224 | + "!" * expect(S * m.V"Prefix", "ExpPatt3") / mt.__unm |
diff --git a/testrelabelparser.lua b/testrelabelparser.lua index 2ca8528..0399ff6 100644 --- a/testrelabelparser.lua +++ b/testrelabelparser.lua | |||
| @@ -53,26 +53,14 @@ L1:C5: unexpected characters after the pattern | |||
| 53 | 53 | ||
| 54 | -- testing ExpPatt1 | 54 | -- testing ExpPatt1 |
| 55 | 55 | ||
| 56 | testerror([['p' //{1}]], [[ | ||
| 57 | L1:C10: expected a pattern after '/' and '//{...}', or the label(s) after '/{' and '//{' | ||
| 58 | 'p' //{1} | ||
| 59 | ^ | ||
| 60 | ]]) | ||
| 61 | |||
| 62 | testerror([['p' //{1} //{2} 'q']], [[ | ||
| 63 | L1:C10: expected a pattern after '/' and '//{...}', or the label(s) after '/{' and '//{' | ||
| 64 | 'p' //{1} //{2} 'q' | ||
| 65 | ^ | ||
| 66 | ]]) | ||
| 67 | |||
| 68 | testerror([['p' /]], [[ | 56 | testerror([['p' /]], [[ |
| 69 | L1:C6: expected a pattern after '/' and '//{...}', or the label(s) after '/{' and '//{' | 57 | L1:C6: expected a pattern after '/' |
| 70 | 'p' / | 58 | 'p' / |
| 71 | ^ | 59 | ^ |
| 72 | ]]) | 60 | ]]) |
| 73 | 61 | ||
| 74 | testerror([['p' / / 'q']], [[ | 62 | testerror([['p' / / 'q']], [[ |
| 75 | L1:C6: expected a pattern after '/' and '//{...}', or the label(s) after '/{' and '//{' | 63 | L1:C6: expected a pattern after '/' |
| 76 | 'p' / / 'q' | 64 | 'p' / / 'q' |
| 77 | ^ | 65 | ^ |
| 78 | ]]) | 66 | ]]) |
| @@ -249,22 +237,22 @@ L1:C2: expected a pattern or closing '}' after '{' | |||
| 249 | ^ | 237 | ^ |
| 250 | ]]) | 238 | ]]) |
| 251 | 239 | ||
| 252 | -- testing ExpNum | 240 | -- testing ExpNumName |
| 253 | 241 | ||
| 254 | testerror([['p' ^ n]], [[ | 242 | testerror([['p' ^ n]], [[ |
| 255 | L1:C6: expected a number after '^', '+' or '-' (no space) | 243 | L1:C6: expected a number, '+', '-' or a name (no space) after '^' |
| 256 | 'p' ^ n | 244 | 'p' ^ n |
| 257 | ^ | 245 | ^ |
| 258 | ]]) | 246 | ]]) |
| 259 | 247 | ||
| 260 | testerror([['p'^+(+1)]], [[ | 248 | testerror([['p'^+(+1)]], [[ |
| 261 | L1:C5: expected a number after '^', '+' or '-' (no space) | 249 | L1:C5: expected a number, '+', '-' or a name (no space) after '^' |
| 262 | 'p'^+(+1) | 250 | 'p'^+(+1) |
| 263 | ^ | 251 | ^ |
| 264 | ]]) | 252 | ]]) |
| 265 | 253 | ||
| 266 | testerror([['p'^-/'q']], [[ | 254 | testerror([['p'^-/'q']], [[ |
| 267 | L1:C5: expected a number after '^', '+' or '-' (no space) | 255 | L1:C5: expected a number, '+', '-' or a name (no space) after '^' |
| 268 | 'p'^-/'q' | 256 | 'p'^-/'q' |
| 269 | ^ | 257 | ^ |
| 270 | ]]) | 258 | ]]) |
| @@ -351,25 +339,18 @@ L1:C2: expected the name of a rule after '<' (no space) | |||
| 351 | 339 | ||
| 352 | -- testing ExpLab1 | 340 | -- testing ExpLab1 |
| 353 | 341 | ||
| 354 | testerror([['p' //{} 'q']], [[ | 342 | testerror([['p' %{} 'q']], [[ |
| 355 | L1:C8: expected at least one label after '{' | 343 | L1:C7: expected a label after '{' |
| 356 | 'p' //{} 'q' | 344 | 'p' %{} 'q' |
| 357 | ^ | 345 | ^ |
| 358 | ]]) | 346 | ]]) |
| 359 | 347 | ||
| 360 | testerror([[%{ 'label' }]], [[ | 348 | testerror([[%{ 'label' }]], [[ |
| 361 | L1:C3: expected at least one label after '{' | 349 | L1:C3: expected a label after '{' |
| 362 | %{ 'label' } | 350 | %{ 'label' } |
| 363 | ^ | 351 | ^ |
| 364 | ]]) | 352 | ]]) |
| 365 | 353 | ||
| 366 | -- testing ExpLab2 | ||
| 367 | |||
| 368 | testerror([['p' //{1,2,3,} 'q']], [[ | ||
| 369 | L1:C14: expected a label after the comma | ||
| 370 | 'p' //{1,2,3,} 'q' | ||
| 371 | ^ | ||
| 372 | ]]) | ||
| 373 | 354 | ||
| 374 | -- testing ExpNameOrLab | 355 | -- testing ExpNameOrLab |
| 375 | 356 | ||
