jddurand / MarpaX-Languages-ECMAScript-AST

ECMAScript AST using Marpa
2 stars 1 forks source link

StringLiteral: documentation and behaviour mismatch #2

Closed lucs closed 10 years ago

lucs commented 10 years ago

According to MarpaX/Languages/ECMAScript/AST/Grammar/ECMAScript_262_5/Template.pm, we should have:

Transpilation of G1 rule No 3, i.e. Literal ::= StringLiteral
...
Transpilation of G1 rule No 234, i.e. StringLiteral ::= STRINGLITERAL

Yet, it turns out that the code behaves like if it was:

Transpilation of G1 rule No 3, i.e. Literal ::= STRINGLITERAL

as can be shown with the following program:

use strict;
use warnings;
use MarpaX::Languages::ECMAScript::AST;

use Data::Dumper;
$Data::Dumper::Indent = 1;                                                                                                  $Data::Dumper::Quotekeys = 0;
$Data::Dumper::Sortkeys = 1;
print Dumper(MarpaX::Languages::ECMAScript::AST->new->parse(q<"foo";>));

    # If you prefer less verbose output.
#use YAML;
#print YAML::Dump(MarpaX::Languages::ECMAScript::AST->new->parse(q<"foo";>));

Which one should it be?

lucs commented 10 years ago

(Sorry, but be careful, that code has some unitintended whitespace that messes it up a bit.)

jddurand commented 10 years ago

Take this latest source for Progra/Semantics, this is adding lhs and rhs information. StringLiteral is viewed as a lexeme in Program, so it is Literal that applies.

{
    lhs => 'Literal',
    rhs => [
    'StringLiteral'
    ],
    ruleId => 3,
    values => [
        [
         0,
         5,
         'foo'
        ]
    ]
}

In the AST, lexemes are always refs to arrays, while rules are always refs to hash.

jddurand commented 10 years ago

Please note this is not tagged - maybe I'll switch back to a bless => ::lhs method - nevertheless I putted this just to allow you to progress in your use of MarpaX::Languages::ECMAScript::AST -;

jddurand commented 10 years ago

./.. and values are always ref to an array containing one or the other

jddurand commented 10 years ago

Another note: for traveral of the AST you will liekely run into a stack perl limit. The stack-free method to traverse an enormous AST is something like e.g. https://github.com/jddurand/MarpaX-Languages-C-AST/blob/master/lib/MarpaX/Languages/C/AST/Util/Data/Find.pm

jddurand commented 10 years ago

Available as-is with 7a7336b39f997db2d368207007b78e2d86ba1c90, indexed version 0.017