Categories
Posts in this category
- A shiny perl6.org site
- Creating an entry point for newcomers
- Sprixel, a 6 compiler powered by JavaScript
- Another perl6.org iteration
- Blackjack and Perl 6
- Why I commit Crud to the Perl 6 Test Suite
- Report from the Perl 6 Hackathon in Copenhagen
- Custom operators in Rakudo
- Defined Behaviour with Undefined Values
- Dissecting the "Starry obfu"
- Perl 6: Failing Softly with Unthrown Exceptions
- The first Perl 6 module on CPAN
- Google Summer of Code Mentor Recap
- Building a Huffman Tree With Rakudo
- Immutable Sigils and Context
- Is Perl 6 really Perl?
- Perl 6: Lost in Wonderland
- Lots of momentum in the Perl 6 community
- Musing and the future of feather and the Pugs repository
- Musings on Rakudo's spectest chart
- My first executable from Perl 6
- Trying to implement new operators - failed
- Let's build an object
- Perl 6 is optimized for fun
- How to get a parse tree for a Perl 6 Program
- Perl 6 in 2009
- Perl 6 ticket life cycle
- The Perl 6 Advent Calendar
- How to Plot a Segment of a Circle with SVG
- Publicity for Perl 6
- Rakudo architectural overview
- Rakudo Rocks
- Rakudo "star" announced
- Rakudo's rough edges
- Rats and other pets
- Releasing Rakudo made easy
- Set Phasers to Stun!
- Starry Perl 6 obfu
- Recent Perl 6 Developments August 2008
- Strings and Buffers
- Subroutines vs. Methods - Differences and Commonalities
- A SVG plotting adventure
- A Syntax Highlighter for Perl 6
- Test Suite Reorganization: How to move tests
- The Happiness of Design Convergence
- Perl 6 Tidings from September and October 2008
- Perl 6 Tidings for November 2008
- Perl 6 Tidings from December 2008
- Perl 6 Tidings from January 2009
- Perl 6 Tidings from February 2009
- Perl 6 Tidings from March 2009
- Perl 6 Tidings from April 2009
- Perl 6 Tidings from May 2009
- Perl 6 Tidings from May 2009 (second iteration)
- Perl 6 Tidings from June 2009
- Perl 6 Tidings from August 2009
- Perl 6 Tidings from October 2009
- Timeline for a syntax change in Perl 6
- Visualizing match trees
- We write a Perl 6 book for you
- When we reach 100% we did something wrong
- Where Rakudo Lives Now
- Why was the Perl 6 Advent Calendar such a Success?
- What you can write in Perl 6 today
- Why you don't need the Y combinator in Perl 6
Fri, 02 Jan 2009
How to get a parse tree for a Perl 6 Program
Permanent link
Only Perl 6 can parse Perl 6, or so people say. This is mostly true, since
the only parsers that can handle all known Perl 6 code is the module
STD.pm written in Perl 6.
To get access to that parser, all you have to do is get a checkout of a
directory of the pugs repository, linux with perl-5.10.0 or newer in the
location /usr/local/bin/perl and some
modules, like for example Moose and YAML::Syck. When you have
that, type the following commands:
$ svn co http://svn.pugscode.org/pugs/src/perl6 $ cd perl6 $ make
Now the simplest way to check the syntax of a Perl 6 file is to use the
program tryfile:
$ echo 'my ($x, $y) = 1..2;' > test.t $ ./tryfile test.t 00:02 85m
The output might be a bit confusing at first. Since no error message was
given, it means that no syntax error was found. The 00:02 is the
parsing time, and 85 is the memory usage.
Suppose we introduce a syntax error in our test file:
$ echo 'my ($x, $y) = 1..; 2' > wrong.pl $ ./tryfile wrong.pl ############# PARSE FAILED ############# Can't understand next input--giving up at wrong.pl line 1: ------> my ($x, $y) = 1..; 2 expecting any of: prefix or noun whitespace 00:02 85m
The green part of the output indicates the part where the syntax is still
correct, the red part is where the erroneous part begins. (The bold font
weight isn't part of the output, though). At this point, either an
prefix or noun would have been expected, as in 1..+2
or as in 1..2, or a whitespace, probably followed by another
term.
But if you want to known not only if an expression was parsed, but also
how, then there's another way: There's a syntax highlither based on
STD.pm which generates HTML output that also encodes the parse
tree:
$ ./STD_syntax_highlight --full-html=test.html test.t
Open the generated file, test.html (click here
for an example) in your favourite browser, enable Javascript, and click on
the button labled Show Syntax Tree. Then when you hoover with the
mouse over a piece of the code, on the right of the browser window a list is
displayed that shows the calling hirarchy of rules in STD.pm.
If you move the mouse to the test my, you'll see that the top
most parsing statement was statementlist, which then called
statement, which in turn called statement_modexpr
and so on.
You might also notice that scope_declarator calls
scope_declarator__S_905my, which is slightly misleading. In fact
token scope_declarator is a so called proto rule, which means
that multiple rules have the same name, and if you call that rule, it tries to
match all of them in parallel, and the longest match is picked. From these
proto rules the compiler constructs automatically subrules whose names all
contain the double underscore __.
So you should actually read the two rules scope_declarator and
scope_declarator__S_905my as one, where the alternative that
contains the my matched.
There are other possible ways to get a parse tree (for example modifying
tryfile to emit a YAML parse tree, or using Rakudo's
--target=parse option), but most of them produce somewhat verbose
output that's hard to use.