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

template error: attribute parser isn't defined #958

Closed
akonsu opened this issue Jul 21, 2015 · 6 comments
Closed

template error: attribute parser isn't defined #958

akonsu opened this issue Jul 21, 2015 · 6 comments
Assignees
Milestone

Comments

@akonsu
Copy link

akonsu commented Jul 21, 2015

Grammar:

grammar Language;

@lexer::members {var blah = true;}

P : T ',' T ;
T : [a-z]+ {blah}? ;
init: P EOF ;

generates an error below when using Javascript target (antlr4 -Dlanguage=JavaScript Language.g4):

warning(22):  template error: context [/LexerFile /Lexer /dumpActions /RuleSempredFunction] 1:4 attribute parser isn't defined 
@Hugheth
Copy link

Hugheth commented Oct 13, 2015

I too have this error when compiling the ANTLR Java8.g4 grammar with

 antlr4 -Dlanguage=JavaScript Java8.g4

Using version 4.5.1

@BurtHarris
Copy link

BurtHarris commented Aug 11, 2016

Well, I ran into this same message when I tried to add semantic predicates to my lexer. Digging into it some, I think that the fact the error message is referring to 'attribute parser' may be a clue as to the cause. It should be generating a lexer when this occurs.

I think I found the relevant part of the string template in JavaScript.stg, which says:

/* This generates a private method since the predIndex is generated, making an
 * overriding implementation impossible to maintain.
 */
RuleSempredFunction(r, actions) ::= <<
<if(parser)><parser.name><else><lexer.name><endif>.prototype.<r.name>_sempred = function(localctx, predIndex) {
    switch(predIndex) {
        <actions:{index| case <index>:
    return <actions.(index)>;}; separator="\n">
        default:
            throw "No predicate with index:" + predIndex;
    }
};

>>

I don't know STRINGTEMPLATE very well, but it seems like the <else> part of this construct should be rendered, but that it's not getting there. Could the problem be that parser is considered true(ish), but that parser.name is undefined?

Note: the generated code seem to have the appropriate name none-the-less.

@ericvergnaud
Copy link
Contributor

Hi,
from my tests, antlr generates a warning, not an error, and the generated code is correct.
Can you confirm?

@ericvergnaud
Copy link
Contributor

@parrt can you look at my fix-#958 branch, I've tried to get rid of the warning, but it seems that ST can't find the 'parserName' attribute in the LexerFile object model. Not sure what I'm doing wrong...

@parrt
Copy link
Member

parrt commented Dec 13, 2016

I get this error

net.js:623
    throw new TypeError('invalid data');
    ^

TypeError: invalid data
    at Socket.write (net.js:623:11)
    at main (/private/var/folders/93/9kzk2ccm8xj8k70059b28jk80000gp/T/BaseNodeTest-main-1481655380368/Test.js:12:20)
    at Object.<anonymous> (/private/var/folders/93/9kzk2ccm8xj8k70059b28jk80000gp/T/BaseNodeTest-main-1481655380368/Test.js:15:1)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Function.Module.runMain (module.js:447:10)
    at startup (node.js:146:18)
    at node.js:404:3

From

lexer grammar T;

options {language=JavaScript;}

T : 'a' {<True()>}? ;

@parrt
Copy link
Member

parrt commented Dec 13, 2016

@ericvergnaud unfortunately either lexer or parser will be undefined. I think I faked it for C++ as:

<if (r.factory.g.lexer)><lexer.name><else><parser.name><endif>

I'll make the patch and add a test. Yep, that fixes it. I'll push but we still have that net.js issue

@parrt parrt closed this as completed in a3aa610 Dec 13, 2016
parrt added a commit that referenced this issue Dec 13, 2016
get recognizer name properly. Fixes #958
@parrt parrt added this to the 4.6 milestone Dec 13, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants