Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to collect parse results #39

Open
ognevsky opened this issue Jan 17, 2012 · 1 comment
Open

Unable to collect parse results #39

ognevsky opened this issue Jan 17, 2012 · 1 comment

Comments

@ognevsky
Copy link

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.

@ognevsky
Copy link
Author

And one more thing: if I define #value method for my rules, I cannot get this value anymore. So, in my results I always got header\ncontent text, but not result of #value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant