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

C.parse() keeps internal state #6

Open
ghazel opened this issue Feb 17, 2014 · 3 comments
Open

C.parse() keeps internal state #6

ghazel opened this issue Feb 17, 2014 · 3 comments

Comments

@ghazel
Copy link

ghazel commented Feb 17, 2014

C.parse seems to keep state somewhere, which breaks the ability to parse more than once;

1.9.3p286 :001 > require 'cast'
 => true 
1.9.3p286 :002 > C.parse("typedef int fun;")
 => TranslationUnit
    entities: 
        - Declaration
            storage: typedef
            type: Int
            declarators: 
                - Declarator
                    name: "fun"

1.9.3p286 :003 > C.parse("typedef int fun;")
C::ParseError: 1:0: invalid type specifier combination: int fun
    from c.y:593:in `parse'
    from cast-0.2.1/lib/cast/parse.rb:57:in `parse'
    from cast-0.2.1/lib/cast/parse.rb:51:in `parse'
    from (irb):3
    from ruby-1.9.3-p286/bin/irb:16:in `<main>'
@oggy
Copy link
Owner

oggy commented Feb 18, 2014

Yes, as noted in the readme, C parsers need to keep the list of type names as part of their state. Seems like it's useful behavior not to flush this state between parses - it means you can add some type names to the default parser, then parse all the snippets you want. I'm open to being convinced otherwise, but the behavior is intentional.

If you want to control state explicitly, you can instantiate your own parser (C::Parser.new). You can also pass this to the various snippet parsing methods:

2.1.0 :002 > parser = C::Parser.new
 => #<C::Parser:0x00000101f87120 @type_names=#<Set: {}>, @warning_proc=#<Proc:[email protected]:579 (lambda)>, @pos=#<C::Node::Pos:0x00000101f86fe0 @filename=nil, @line_num=1, @col_num=0>> 
2.1.0 :003 > parser.type_names << 'Thing'
 => #<Set: {"Thing"}> 
2.1.0 :004 > C::Declaration.parse('Thing t;', parser)
 => Declaration
    type: CustomType
        name: "Thing"
    declarators: 
        - Declarator
            name: "t"

@ghazel
Copy link
Author

ghazel commented Feb 18, 2014

I would expect module / class methods to keep no state, and the C::Parser.new approach to be used when state should be kept on that object.

@oggy
Copy link
Owner

oggy commented Feb 18, 2014

Ok, I'm convinced. :)

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

2 participants