libdparse icon indicating copy to clipboard operation
libdparse copied to clipboard

Attributes broken?

Open maxhaton opened this issue 6 years ago • 1 comments

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);
}

maxhaton avatar Sep 12 '19 03:09 maxhaton

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: []

CyberShadow avatar Sep 12 '19 23:09 CyberShadow