Jump to content
  • Advertisement
Sign in to follow this  
nukomod

Boost & Spirit : Need help getting file position.

This topic is 4774 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've written a parser in Spirit that reads through a file and builds an abstract syntax tree from the data. The program is an editor that then uses this tree to make all it's data structures etc. The user edits everything and then needs to save the changes back to the file. This is the problem, I need to know where to save the changes to. I can't delete and rewrite the entire file because the data is embedded amongst lots of other stuff. If I can just get the position in the file where each successful match was made for each node in the tree then I could use that to write the modified data directly to the correct places in the file. I've looked into the position iterator that comes with Spirit but cannot get it to compile when using my grammar. The code below is where I think the trouble is (right now the whole file is loaded into 'str'). The compiler throws many errors but the clue is: cannot convert from 'const iterator_t' to 'const char *' typedef position_iterator<char const*> iterator_t; iterator_t begin(str, str + strlen(str), file); iterator_t end; tree_parse_info<> info2 = ast_parse(begin, end, state_g, space_p); This is likely a simple syntax error someplace, probably in my grammar declaration, but I need some help tracking it down. If there's another approach I could take to do this I'd be very grateful to hear it!

Share this post


Link to post
Share on other sites
Advertisement
I'm not entirely sure, but position_iterator<char const*> looks suspect. I just looked at the documentation, and it said "It requires a single template parameter, which should be the type of the iterator that is to be wrapped."

Perhaps you could try passing file_iterator <char_t> as the template parameter?

Share this post


Link to post
Share on other sites
Quote:
Original post by Oxyacetylene
I'm not entirely sure, but position_iterator<char const*> looks suspect. I just looked at the documentation, and it said "It requires a single template parameter, which should be the type of the iterator that is to be wrapped."


Pointer types are a model of the Iterator concept, a pointer is a form of iterator and can be used in place of an iterator in generic programming.

Share this post


Link to post
Share on other sites
Quote:
Original post by snk_kid
Pointer types are a model of the Iterator concept, a pointer is a form of iterator and can be used in place of an iterator in generic programming.


Yeah, but I don't think different iterator types are interchangable in boost::spirit, although I could be wrong there.

i.e. if you instantiate a parser that takes a file iterator, I don't think you can pass it a char pointer instead of a file iterator.

Share this post


Link to post
Share on other sites
At the moment 'str' is a char const* and not a file iterator, I think you're right about spirit not liking interchanging iterator types. As far as I know though everything is just using a char pointer.

The " cannot convert from 'const iterator_t' to 'const char *' " error is thrown up within common.hpp by the way. would any other info help?

Share this post


Link to post
Share on other sites
Quote:
Original post by Oxyacetylene
Quote:
Original post by snk_kid
Pointer types are a model of the Iterator concept, a pointer is a form of iterator and can be used in place of an iterator in generic programming.


Yeah, but I don't think different iterator types are interchangable in boost::spirit, although I could be wrong there.

i.e. if you instantiate a parser that takes a file iterator, I don't think you can pass it a char pointer instead of a file iterator.


No you are wrong. About your example, boost parser functions can take template iterator range any type can be used, typically as long as the type is a model of the Forward Iterator concept.

Quote:

Spirit uses the same iterator concepts and interface formally defined by the C++ Standard Template Library (STL). We can use iterators supplied by STL's containers (e.g. list, vector, string, etc.) as is, or perhaps write our own. Iterators can be as simple as a pointer (e.g. char const*). At the other end of the spectrum, iterators can be quite complex; for instance, an iterator adapter that wraps a lexer such as LEX.


[Edited by - snk_kid on July 26, 2005 8:25:42 AM]

Share this post


Link to post
Share on other sites
I am attempting to write my own scanner iteration policy which will keep track of the position inside the string as it reads each character.. do you think this is a good idea?

Share this post


Link to post
Share on other sites
I think I've cracked it using a rubbish, rubbish, super rubbish work around.


I've setup some semantic actions to catch when certain rules are recognised. The layout of a semantic action is:

void f(IteratorT first, IteratorT last);

The 'first' iterator in my case points to the same const char* buffer the file is loaded into. The address of the buffer minus the address of the first iterator equals the position within the buffer. The length of the parsed 'token' (right name?) is just last-first. This seems to do the trick but I don't likes it I tells ya.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!