Language-FormulaEngine
view release on metacpan or search on metacpan
lib/Language/FormulaEngine/Parser.pm view on Meta::CPAN
sub Language::FormulaEngine::Parser::Node::String::deparse {
_str_escape(shift->string_value);
}
sub new_string {
my ($self, $text)= @_;
bless \$text, 'Language::FormulaEngine::Parser::Node::String'
}
sub Language::FormulaEngine::Parser::Node::Number::new {
my ($class, $value)= @_;
$value= 0+$value;
bless \$value, $class;
}
sub Language::FormulaEngine::Parser::Node::Number::is_constant { 1 }
sub Language::FormulaEngine::Parser::Node::Number::number_value { ${$_[0]} }
sub Language::FormulaEngine::Parser::Node::Number::evaluate { ${$_[0]} }
sub Language::FormulaEngine::Parser::Node::Number::simplify { $_[0] }
sub Language::FormulaEngine::Parser::Node::Number::deparse { shift->number_value }
sub new_number {
my $value= $_[1]+0;
bless \$value, 'Language::FormulaEngine::Parser::Node::Number'
}
sub get_negative {
my ($self, $node)= @_;
return $self->new_number(-$node->number_value) if $node->can('number_value');
return $node->parameters->[0] if $node->can('function_name') and $node->function_name eq 'negative';
return $self->new_call('negative', [$node]);
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Language::FormulaEngine::Parser - Create parse tree from an input string
=head1 VERSION
version 0.08
=head1 SYNOPSIS
my $parse_tree= Language::FormulaEngine::Parser->new->parse($string);
=head1 DESCRIPTION
This class scans tokens from an input string and builds a parse tree. In compiler terminology,
it is both a Scanner and Parser. It performs a top-down recursive descent parse, because this
is easy and gives good error messages. It only parses strings, but leaves room for subclasses
to implement streaming. By default, the parser simply applies a Grammar to the input, without
checking whether the functions or variables exist, but can be subclassed to do more detailed
analysis during the parse.
The generated parse tree is made up of Function nodes (each infix operator is converted to a
named function) and each Function node may contain Symbols, Strings, Numbers, and other
Function nodes. The parse tree can be passed to the Evaluator for instant execution, or passed
to the Compiler to generate an optimized perl coderef. The parse tree is lightweight, and does
not include token/context information; this could also be added by a subclass.
=head1 PUBLIC API
=head2 parse
Parse a new input text, updating all derived attributes with the result of the operation.
It returns the value of L</parse_tree> (which is undef if the parse failed).
On failure, the exception is stored in L</error> and other attributes like L</token_pos> may
contain useful diagnostic information.
=head2 parse_tree
This holds the generated parse tree, or C<undef> if the parse failed. See L</"Parse Nodes">.
=head2 error
This is C<undef> if the parse succeeded, else an error message describing the syntax that ended
the parse.
=head2 functions
A set (hashref) of all function names encountered during the parse.
=head2 symbols
A set (hashref) of all non-function symbols encountered. (variables, constnts, etc.)
=head2 reset
Clear the results of the previous parse, to re-use the object. Returns C<$self> for chaining.
=head2 deparse
my $formula_text= $parser->deparse($tree);
Return a canonical formula text for the parse tree, or a parse tree that you supply.
=head1 EXTENSIBLE API
These methods and attributes are documented for purposes of subclassing the parser.
=head2 input
The input string being scanned.
Code within the parser should access this as C<< $self->{input} >> for efficiency.
=head2 input_pos
Shortcut for C<< pos($self->{input}) >>.
=head2 token_type
( run in 0.357 second using v1.01-cache-2.11-cpan-e1769b4cff6 )