0

Checking to see if I'm doing this right. I think I have a fairly firm grasp on BNF grammars, but don't quite get how the actions are supposed to work. The WORD token below is a string. The tag list should be an array of tags. The other rules should do different things based on the type. If it's the tags object it should make a member of a JSON object called tags. If it's a string it should be a property called title. Is that the right way to move forward?

headerline
  : STAR SPACE WORD headerline
    {$$ = {state: $3, rest: $4}}
  | WORD headerline
    /* Looking for something like if typeof $2 === 'object' add word to title else concat string */
    {$$ = $1 + $2}
  | SPACE headerline
    {$$ = $1 + $2}
  | taglist
    {$$ = {tags: $1}}
  | WORD
    {$$ = $1}
  ;

Example text:

* TODO Use docker for setup :dev:shell:

Side note and other questions:

  • It was super confusing on when to use return $1 vs $$ = $1 in a rule at first.
  • For outdents/indents I've only seen the grammar for CoffeeScript and that seems overly complicated. Can you do that in the vanilla lexer?
Justin Thomas
  • 5,680
  • 3
  • 38
  • 63
  • Often if you find yourself saying things like "The other rules should do different things based on the type," it means that your grammar doesn't actually capture the structure of what you are parsing. – rici Jun 26 '14 at 01:03
  • Somehow that was my gut feeling. Actually got it to work without doing that. – Justin Thomas Jun 26 '14 at 01:26

0 Answers0