Skip to content

Unable to collect parse results #39

@ognevsky

Description

@ognevsky

Sorry for asking here, but nobody approved my question in citrus–users google group, so i'll try ask here (I really need the answer).

So, I have some rules to parse something like this:

header1
content1

header2
content2

header3
content3

And then I need to get an array of hashes:

[{header: 'header1', content: 'content1'}, {header: 'header2', content: 'content2'}, {header: 'header3', content: 'content3'}]

So, I think I wrote right rules, something like this:

grammar Markdown
  rule section_list
    section (ln section_list)?
  end

  rule section
    header ln content
  end

  rule header
    'header'
  end

  rule content
    'content'
  end

  rule ln
    "\n"+
  end
end

When I parse it and run #dump method, it output is:

"header\ncontent\n\nheader\ncontent\n\nheader\ncontent" rule=section (ln section_list)?, offset=0, length=46
 "header\ncontent" rule=section, offset=0, length=14
  "header" rule=header, offset=0, length=6
  "\n" rule=ln, offset=6, length=1
   "\n" rule="\n", offset=6, length=1
  "content" rule=content, offset=7, length=7
 "\n\nheader\ncontent\n\nheader\ncontent" rule=(ln section_list)?, offset=14, length=32
  "\n\nheader\ncontent\n\nheader\ncontent" rule=ln section_list, offset=14, length=32
   "\n\n" rule=ln, offset=14, length=2
    "\n" rule="\n", offset=14, length=1
    "\n" rule="\n", offset=15, length=1
   "header\ncontent\n\nheader\ncontent" rule=section_list, offset=16, length=30
    "header\ncontent" rule=section, offset=16, length=14
     "header" rule=header, offset=16, length=6
     "\n" rule=ln, offset=22, length=1
      "\n" rule="\n", offset=22, length=1
     "content" rule=content, offset=23, length=7
    "\n\nheader\ncontent" rule=(ln section_list)?, offset=30, length=16
     "\n\nheader\ncontent" rule=ln section_list, offset=30, length=16
      "\n\n" rule=ln, offset=30, length=2
       "\n" rule="\n", offset=30, length=1
       "\n" rule="\n", offset=31, length=1
      "header\ncontent" rule=section_list, offset=32, length=14
       "header\ncontent" rule=section, offset=32, length=14
        "header" rule=header, offset=32, length=6
        "\n" rule=ln, offset=38, length=1
         "\n" rule="\n", offset=38, length=1
        "content" rule=content, offset=39, length=7
       "" rule=(ln section_list)?, offset=46, length=0

But if I output #captures method, I see something like this:

{
  0=>"header\ncontent\n\nheader\ncontent\n\nheader\ncontent", 
  :section=>["header\ncontent"], 
  1=>"header\ncontent", 
  :ln=>["\n\n"], 
  :section_list=>["header\ncontent\n\nheader\ncontent"], 
  2=>"\n\nheader\ncontent\n\nheader\ncontent"
}

So, the main question is: how collect my parse results into a hash?

Thanks a lot for your awesome lib, help me please.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions