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

Attributes broken? #370

Open
maxhaton opened this issue Sep 12, 2019 · 1 comment · May be fixed by #466
Open

Attributes broken? #370

maxhaton opened this issue Sep 12, 2019 · 1 comment · May be fixed by #466

Comments

@maxhaton
Copy link

maxhaton commented Sep 12, 2019

Either by using the visitor interface or serializing a whole module, we can see AtAttributes are being parsed (as are access specifiers) but they don't seem to be attached to FunctionDeclaration classes. I could be doing something wrong here, but as far as I can tell it is impossible to get attributes of functions without keeping your own track of the whole module separately

/+dub.sdl:
dependency "libdparse" version="~>0.9"
+/
import dparse.ast;
import std.stdio;
import dparse.lexer;
class TestVisitor : ASTVisitor
{
    alias visit = ASTVisitor.visit;

    override void visit(const FunctionDeclaration decl)
    {
       
        decl.name.text.writeln;
        decl.attributes.writeln; //empty?
      	decl.memberFunctionAttributes.writeln;
        decl.accept(this);
        
        
    }
    override void visit(const AtAttribute decl)
    {
    	writeln("wtf");
    }
    
}

void main()
{
    import dparse.lexer;
    import dparse.parser : parseModule;
    import dparse.rollback_allocator : RollbackAllocator;
    import std.array : array;
    import std.string : representation;

    auto sourceCode = q{
        
        public @safe @wow pure void foo(int x)   
        {
        	pragma(msg, "dumb");
        }
        class test {
        	private const void functor() 
            {
            	
            }
        }
    }.dup;
    LexerConfig config;
    auto cache = StringCache(StringCache.defaultBucketCount);
    auto tokens = getTokensForParser(sourceCode.representation, config, &cache);
	
    RollbackAllocator rba;
    auto m = parseModule(tokens.array, "test.d", &rba);
    auto visitor = new TestVisitor();
    visitor.visit(m);
}
@CyberShadow
Copy link
Member

CyberShadow commented Sep 12, 2019

Looks like libdparse is just weird here. These are parsed as attributes at the Declaration level when they prefix the function name, but as memberFunctionAttributes on the FunctionDeclaration if they postfix it. I.e. this program using input

public @safe @wow pure void prefixAttrs(int x) {}
public void suffixAttrs(int x) @safe @wow pure {}
class test
{
    private const void functor()
    {
    }
}

produces:

Declaration
- .attributes: ["public", "safe", "wow", "pure"]
Attribute: public
Attribute: safe
Attribute: wow
Attribute: pure

FunctionDeclaration: prefixAttrs
- .attributes: []
- .memberFunctionAttributes: []



Declaration
- .attributes: ["public"]
Attribute: public

FunctionDeclaration: suffixAttrs
- .attributes: []
- .memberFunctionAttributes: ["safe", "wow", "pure"]
AtAttribute: safe
AtAttribute: wow



Declaration
- .attributes: []

Declaration
- .attributes: ["private", "const"]
Attribute: private
Attribute: const

FunctionDeclaration: functor
- .attributes: []
- .memberFunctionAttributes: []

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

Successfully merging a pull request may close this issue.

2 participants