kach / nearley

πŸ“œπŸ”œπŸŒ² Simple, fast, powerful parser toolkit for JavaScript.
https://nearley.js.org
MIT License
3.57k stars 232 forks source link

Invalid syntax when using moo #501

Closed pablop94 closed 4 years ago

pablop94 commented 4 years ago

Hi,

I'm new in this parsing world and I've using nearly for an university project. I don't know if I'm doing something wrong or there is an issue in nearley/moo.

Here's my grammar: `@{% const moo = require("moo");

const lexer = moo.compile({ ws: /[ \t]+/, Immediate: /0x[a-fA-F0-9]{4}/, Register: /[rR][0-7]/, OperationT2: ["CALL", "JMP"], OperationT1: ["MOV", "ADD", "SUB", "MUL", "DIV", "CMP"], }); %}

@lexer lexer

InstructionT1 -> %OperationT1 %ws %Immediate %ws %Immediate{% (result) => { return { operation: result[0], source: result[2], target: result[4] }}%}

InstructionT2 -> %OperationT2 %ws %Immediate{% id %} `

I expect that valid strings are: "CALL 0x0000" "MOV 0x0000 0x0000"

When I put "CALL 0x0000" it gives me a syntax error at line 1 col 1. If I delete the type OperationT1, the string "CALL 0x0000" works fine.

Could you please help me? Thanks, Pablo

pablop94 commented 4 years ago

There is a problem in my grammar, I was not defining a Main that applies for both, InstructionT1 and InstructionT2, like

Main -> InstructionT1 | InstructionT2

at the very first definition