Questions tagged [pegjs]

PEG.js is a simple parser generator for JavaScript that produces fast parsers with excellent error reporting. Forked to a now-maintained version called Peggy.

PEG.js is a simple parser generator for JavaScript that produces fast parsers with excellent error reporting.

See https://pegjs.org/

The project was forked to create Peggy, which is now maintained at:

https://peggyjs.org/

123 questions
21
votes
3 answers

Simple parsing questions using PEG.js

I'm trying to wrap my head around PEG by entering simple grammars into the PEG.js playground. Example 1: Input: "abcdef1234567ghijklmn8901opqrs" Desired output: ["abcdef", "1234567", "ghijklmn", "8901", "opqrs"] Actual output: ["abcdef",…
Nick Evans
  • 211
  • 2
  • 3
17
votes
2 answers

Join is not a function

I'm playing around with PEG.js start = keyword keyword = a:[a-z]? {return a.join("");} Why am I getting here the error: a.join is not a function when I enter a valid string like abc?
Evgenij Reznik
  • 17,916
  • 39
  • 104
  • 181
17
votes
1 answer

Generate TextMate language grammar from PEG.js grammar

Is there a tool that translates a PEG.js grammar to a TextMate grammar? I am building my own language and would like to have syntax highlighting for it in my preferred editor, TextMate. The grammar of my language is built with PEG.js. According to…
Manuel Leuenberger
  • 2,327
  • 3
  • 21
  • 27
14
votes
1 answer

TS2307: Cannot find module or its corresponding type declarations

I have a private GitHub repo my-parser-generator which uses pegjs. All underlying logic is written in TypeScript, which is then compiled and fed to pegjs, which generates the final parser. Then this repo a is used a package dependency in another…
tekken
  • 171
  • 1
  • 1
  • 6
13
votes
2 answers

How do you build a left-associative operator tree using PEG.js?

How do you build an AST (Abstract Syntax Tree) for left-associative operators using PEG.js? I've tried to write some code based on the information I found on the internet, but I seem to have made a mistake. The code I wrote generates an incorrect…
Toothbrush
  • 2,080
  • 24
  • 33
12
votes
3 answers

Configure Karma to load pegjs with requirejs

Trying to test a project using PegJS and requirejs. I have a couple of source files, implemented as AMD module (define) which loads through the require API. Below the directory structure: js/ somefile.js main.js parser.js test/ …
Luke Skywalker
  • 1,464
  • 3
  • 17
  • 35
11
votes
2 answers

How does backtracking work in peg.js (with example)?

I've defined the following minimal Peg.js grammar: start = "A1" / "A123" which you can try in the sandbox. I would have expected to match "A1" as well as "A123" (according to my notion of how backtracking works). But this is not the case: the…
Bosh
  • 8,138
  • 11
  • 51
  • 77
9
votes
1 answer

How do I parse this with peg grammar?

I'm trying to make a parser using pegjs. I need to parse something like: blah blah START Lorem ipsum dolor sit amet, consectetur adipiscing elit END foo bar etc. I have trouble writing the rule to catch the text from "START" to "END".
John Smith
  • 4,402
  • 4
  • 32
  • 34
7
votes
1 answer

Parsing boolean expression without left hand recursion

I'm trying to match this f(some_thing) == 'something else' f(some_thing) is a function call, which is an expression == is a boolean operator 'something else' is a string, which also is an expression so the boolean expression should…
gosukiwi
  • 1,569
  • 1
  • 27
  • 44
7
votes
3 answers

Using PEG Parser for BBCode Parsing: pegjs or ... what?

I have a bbcode -> html converter that responds to the change event in a textarea. Currently, this is done using a series of regular expressions, and there are a number of pathological cases. I've always wanted to sharpen the pencil on this grammar,…
Steve Ross
  • 4,134
  • 1
  • 28
  • 40
6
votes
2 answers

How best to parse a comma separate list in PEG grammar

I'm trying to parse a comma separated list. To simplify, I'm just using digits. These expressions would be valid: (1, 4, 3) () (4) I can think of two ways to do this and I'm wondering why exactly the failed example does not work. I believe it is a…
Leon Starr
  • 480
  • 1
  • 4
  • 10
6
votes
1 answer

How to describe function arguments in PEG grammar

I'm still fighting with ambiguous grammar of Qt's qmake. Now I can't find a way to describe function arguments that can contain parenthesis (e.g. regex): functionName(arg1, "arg2", ^(arg3)+$) I've tried to describe function call like…
eraxillan
  • 1,552
  • 1
  • 19
  • 40
5
votes
1 answer

PEG.js Get any text between ( and );

I'm trying to catch some text between parathesis with a semicolon in the end. Example: (in here there can be 'anything' !"#¤);); any character is possible); I've tried this: Text = "(" text:(.*) ");" { return text.join(""); } But it seems (.*)…
mottosson
  • 3,283
  • 4
  • 35
  • 73
5
votes
2 answers

Allowing for quotes and unicode in PEG.js grammar definitions

How would one allow for single and double quotes, as well as unicode characters inside of a PEG.js grammar definition? To be more specific, I'd like to be able to capture strings that can contain both single and double quotes (will most likely have…
Speedy
  • 467
  • 1
  • 6
  • 16
5
votes
1 answer

How do you parse nested comments in pegjs?

I was wondering how do you parse comments (say, a la Haskell), in pegjs. The goal: {- This is a comment and should parse. Comments start with {- and end with -}. If you've noticed, I still included {- and -} in the comment. This…
Hassan Hayat
  • 1,056
  • 8
  • 20
1
2 3
8 9