peggyjs / peggy

Peggy: Parser generator for JavaScript

Home Page:https://peggyjs.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

use `expression1 / expression2 / ... / expressionn` throw `peg$SyntaxError: Expected end of input but "d" found.`

wszgrcy opened this issue · comments

commented
INPUT =value1:'aabbcc' / value2:.*{
  if (value1) {
    return {value:value1,type:1}
  }else {
    return {value:value2,type:2}
  }
}

I want match rule1 or rule2 or ... and return a object {value,type}
if input aabbcc(less or equal than first expression) ,can get output,
but if input aabbccd(greater than first expression), throw error

peg$SyntaxError: Expected end of input but "d" found.
    at new peg$SyntaxError (eval at compile (E:\Code\TEST\node_modules\peggy\lib\compiler\index.js:129:16), <anonymous>:14:20)
    at peg$buildStructuredError (eval at compile (E:\Code\TEST\node_modules\peggy\lib\compiler\index.js:129:16), <anonymous>:399:12)   
    at Object.peg$parse [as parse] (eval at compile (E:\Code\TEST\node_modules\peggy\lib\compiler\index.js:129:16), <anonymous>:476:11)
    at test (E:\Code\TEST\peggy\index.ts:23:25)
    at Object.<anonymous> (E:\Code\TEST\peggy\index.ts:27:1)
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Module.m._compile (E:\msys2\home\tom\.nvm\versions\node\v16.14.0\bin\node_modules\ts-node\src\index.ts:1618:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
    at Object.require.extensions.<computed> [as .ts] (E:\msys2\home\tom\.nvm\versions\node\v16.14.0\bin\node_modules\ts-node\src\index.ts:1621:12)
    at Module.load (node:internal/modules/cjs/loader:981:32) {
  expected: [ { type: 'end' } ],
  found: 'd',
  location: {
    source: undefined,
    start: { offset: 6, line: 1, column: 7 },
    end: { offset: 7, line: 1, column: 8 }
  }
}

Is there a problem with me using it?
I think /is like rule1||rule2|...||rule,But it seems like partial success is considered a success

commented

I add !. to sovled ,but I don't know the / real purpose

a / b tries a, and if it matches, then the whole rule matches without trying b. At a higher level, if there is input left when your grammar's first rule matches, that's an error.

commented

a / b tries a, and if it matches, then the whole rule matches without trying b. At a higher level, if there is input left when your grammar's first rule matches, that's an error.

not match and match but not full match are two different status? so I need to convert match but not full match=>not match (add !. not match any char in end)

If you want to match just the beginning part of text, and don't care if there is extra text at the end, you could do:

INPUT 
  = value1:'aabbcc' .*
  / value2:.*

I am assuming this fixed your issue. If not, please re-open, and ask more questions.