storing line numbers of expressions with boost.spi

2019-02-16 13:24发布

问题:

I am planning on doing a script transformation utility (for extended diagnostic information) using Boost.Spirit 2.

While there is support for line information etc. for parsing errors, how i can store line numbers for successfully parsed expressions with Qi?

回答1:

As per the mailing list, Spirit.Classic positional iterators can also be used with Spirit 2.
There is also an article on an iter_pos-parser on the Spirit-blog.

I will update when i had time to test.



回答2:

I realize I noticed the question late, but let me add this anyway. I wrote a sample of an INI file parser in another answer:

  • Cross-platform way to get line number of an INI file where given option was found

This uses a 'simple' semantic action with line_pos_iterator.

Here is the fruit of my labour: https://gist.github.com/1425972

  • When POSITIONINFO == 0
    • input is streaming
    • output is raw strings (well, map<string, map<string, string> > for the sections)
  • When POSITIONINFO == 1

    • input is buffered
    • output is textnode_t:

      struct textnode_t {
          int sline, eline, scol, ecol;
          string_t text;
      };
      

      This means that the resulting map<textnode_t, map<textnode_t, textnode_t> > is able to report exactly what (line,col) start and end points mark the individual text nodes.

Here is a reduced demo. For full description and extensive test cases see the original anser or the code at github

Demo Input

[Cat1]
name1=100 #skipped

name2=200 \#not \\skipped
name3=   dhfj dhjgfd

Demo Output (POSITIONINFO == 0)

Parse success!
[Cat1]
name1 = 100 
name2 = 200 \#not \\skipped
name3 = dhfj dhjgfd

Demo Output (POSITIONINFO == 1)

Parse success!
[[L:1,C2 .. L1,C6:Cat1]]
[L:2,C2 .. L2,C7:name1] = [L:2,C8 .. L2,C12:100 ]
[L:6,C2 .. L6,C7:name2] = [L:6,C8 .. L6,C27:200 \#not \\skipped]
[L:7,C2 .. L7,C7:name3] = [L:7,C11 .. L7,C22:dhfj dhjgfd]


回答3:

Here is another useful article that explains how to use the exception that the phrase_parse function throws.

The article describes how to get error messages like this:

Exception: parse error at file STDIN line 1 column 10
'123,42.0,a,1.4'
          ^- here