P6C::Parser ^

The parser. It provides the following methods:

new

Create a new parser object from scratch. This is slow.

Precompile($name)

Call Parse::RecDescent's Precompile method to compile the grammar to the file $name.pm. The resulting parser can then be loaded as a module, and instantiated with its new method. This is much faster than re-building the grammar.

Debug($on)

If $on is true, generate line-number information. This actually has a surprisingly small impact on performance. If $on is false, turn line-number generation off.

Reset()

Reset global parser state.

It's been tweaked for speed in a number of ways. First, the infix operators have been turned into regexes. Second, %item is not used, and the grammar can be built without %item support for more speed. Third, a number of logically distinct rules have been inlined, making the job of postprocessing the parse tree somewhat more involved.

Internals ^

%WANT

Maps function (prefix operator) names to their corresponding argument context rules. The entries serve the dual purpose of recognizing a prefix operator, and of telling the parser what to parse next.

$FUNCTION_ARGS

The default argument context rule; a comma-separated list of values.

%CLASSES

Has an entry for each class. This allows bare class-names to be recognized. The hash values are currently unimportant.

add_function($function, $params, $parser)

Called in function declarations and definitions to add a function to the parser.

add_class($class)

Add a class.

set_error_handler($err_handler)

Sets the error handler for the parser. When an error is detected during the parse, $err_handler is called with these parameters: &$err_handler($msg, $linenum)

If no error handler if set then a message is printed on STDERR.

This function returns the previous error handler.

parse_sig

Parses a signature and returns (signature, context).

argument_context($name, $params, $parser)

Abandon hope, all ye who enter here. Given the first part of a sub definition, this code adds a rule to the parser to handle its argument context, then creates and eval's a function to handle objects of the resulting type. Yes, it's ugly. No, I don't see a less ugly way to do it.

argtype($thing)

Given a P6C::variable, return the appropriate syntax rule to recognize an argument of that time. Currently always returns "scalar_expr".

optional_last($n)

Create code to extract arguments from a (',' thing)(s?) element at position $n and flatten it.


parrot