Skip to content

Instantly share code, notes, and snippets.

@acdcjunior
Created February 21, 2016 06:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acdcjunior/cfee862d1618e693bdc6 to your computer and use it in GitHub Desktop.
Save acdcjunior/cfee862d1618e693bdc6 to your computer and use it in GitHub Desktop.
Parsing brackets using JavaScript (with PEG.js, not regex)

GO TO

  http://pegjs.org/online

Paste in the grammar:

  Expression
        = head:Factor tail:Factor* {
              return head + tail.join("");
        }
  Factor
        = "[" expr:Expression "]" { return "[<em>"+expr+"</em>]"; }
        / RegularChar
  RegularChar
        = [^\[\]]+ { return text(); }

Add some text in the input box

Such as:

[test1] test2 ([[readonly ][optionality ][argumentType ]argumentName[ = defaultValue]]...) [test3] test4

Notice it breaks when there are unclosed [ or unopened ]. Try:

[this one is closed] but [this one is not

Watch the output

If it is OK, then go and generate the .js. If not, edit the grammar until so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment