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

Block style conditional statements in actions #85

Closed
ixti opened this issue Feb 21, 2012 · 6 comments
Closed

Block style conditional statements in actions #85

ixti opened this issue Feb 21, 2012 · 6 comments
Assignees

Comments

@ixti
Copy link

ixti commented Feb 21, 2012

Right now one need to write cond. stmts like this:

if (yytext.slice(-1) === '\\')
  yytext = yytext.substr(0, yyleng-1),
  do_more
else
  do_other;

it would be great to be able to use braces :))

@jasonwyatt
Copy link

You can use braces if you also use double-braces to wrap your action:

{{
    if (ytext.slice(-1) === '\\') {
        yytext = yytext.substr(0, yyleng-1),
        // do more
    } else {
        // do other
    }
}}

Hope this helps!

@ghost ghost assigned zaach Mar 2, 2012
@ixti
Copy link
Author

ixti commented Mar 3, 2012

@jasonwyatt Thanks. That works for parser. But not for lexer...

To be more visual, here's what I mean. Following works perfectly:

%lex

%%

.* {
     return "FOOBAR";
   }

/lex

%start test

%%

test
  : FOOBAR EOF {{
    if (true) {
      console.log('test');
    }

    return $$;
  }}
  ;

But this one fails:

%lex

%%

.* {{
     if (true) {
      console.log('test');
     }
     return "FOOBAR";
   }}

/lex

%start test

%%

test
  : FOOBAR EOF {{
    if (true) {
      console.log('test');
    }

    return $$;
  }}
  ;

The last one gives me following on attempt to compile:

node.js:201
        throw e; // process.nextTick error, or 'error' event on first tick
              ^
SyntaxError: Unexpected token case
    at Function (unknown source)
    at Object.buildActions (/home/ixti/projects/ndoc/node_modules/jison/lib/jison/lexer.js:111:12)
    at Object.RegExpLexer (/home/ixti/projects/ndoc/node_modules/jison/lib/jison/lexer.js:122:39)
    at [object Object].Jison_Generator (/home/ixti/projects/ndoc/node_modules/jison/lib/jison.js:102:22)
    at [object Object].<anonymous> (/home/ixti/projects/ndoc/node_modules/jison/lib/jison/util/typal.js:23:28)
    at new <anonymous> (/home/ixti/projects/ndoc/node_modules/jison/lib/jison/util/typal.js:77:70)
    at new Jison_Generator (/home/ixti/projects/ndoc/node_modules/jison/lib/jison.js:1527:20)
    at processGrammar (/home/ixti/projects/ndoc/node_modules/jison/lib/jison/cli-wrapper.js:76:21)
    at Object.main (/home/ixti/projects/ndoc/node_modules/jison/lib/jison/cli-wrapper.js:48:46)
    at Object.<anonymous> (/home/ixti/projects/ndoc/node_modules/jison/lib/jison/cli-wrapper.js:81:13)

@jasonwyatt
Copy link

@ixti Ah, after looking through the source a bit, I think this is how you can do multi-line actions with braces in the lexer config:

.* %{
        if (true) {
            console.log('test');
        }
        return "FOOBAR";
    %}

@ixti
Copy link
Author

ixti commented Mar 3, 2012

Oops. You're right! I was trying it before but it as not working - seems I was missing something (probably I was misspelling closing brace)

@ixti ixti closed this as completed Mar 3, 2012
@ixti
Copy link
Author

ixti commented Mar 3, 2012

@jasonwyatt thanks!

@zaach zaach reopened this Mar 4, 2012
@zaach
Copy link
Owner

zaach commented Mar 4, 2012

I'll reopen this as a reminder that I still need to implement block style with single braces.

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

3 participants