with Tokens.Scanners; use Tokens.Scanners;
package Expressions.Parsers is
-- An Expression_Parser parses an Expression from a token scanner. Given a
-- token scanner loaded with an input stream, the expression parser will
-- read tokens from the scanner and create a corresponding Expression tree.
type Expression_Parser is new Limited_Object with private;
type A_Expression_Parser is access all Expression_Parser'Class;
-- Creates and returns a new Expression_Parser.
function Create_Expression_Parser return A_Expression_Parser;
-- Scans an expression or raises Parse_Exception if one is not found.
-- Parse_Exception will be raised if a parsing error occurs. Token_Exception
-- will be raised if an unrecognized token is encountered by the scanner.
function Expect_Expression( this : not null access Expression_Parser'Class;
scanner : not null A_Token_Scanner ) return A_Expression;
pragma Postcondition( Expect_Expression'Result /= null );
-- Scans an expression or returns null if one is not found. Parse_Exception
-- will be raised if a parsing error occurs. Token_Exception will be raised
-- if an unrecognized token is encountered by the scanner.
function Scan_Expression( this : not null access Expression_Parser'Class;
scanner : not null A_Token_Scanner ) return A_Expression;
-- Deletes the Expression_Parser.
procedure Delete( this : in out A_Expression_Parser );
private
type Expression_Parser is new Limited_Object with null record;
-- Scans an expression or raises Parse_Exception if one is not found. If a
-- boolean operator is encountered, it must have an operator precedence
-- greater than or equal to 'precedence'. Parse_Exception will be raised if
-- a parsing error occurs. Token_Exception will be raised if an unrecognized
-- token is encountered by the scanner.
--
-- The grammar implemented by this function is:
-- expression := <term> <binary_op> <term>
-- expression := <term>
function Expect_Expression( this : not null access Expression_Parser'Class;
scanner : not null A_Token_Scanner;
precedence : Natural ) return A_Expression;
pragma Postcondition( Expect_Expression'Result /= null );
-- Scans an expression or returns null if one is not found. If a boolean
-- operator is encountered, it must have an operator precedence greater than
-- or equal to 'precedence'. Parse_Exception will be raised if a parsing
-- error occurs. Token_Exception will be raised if an unrecognized token is
-- encountered by the scanner.
--
-- The grammar implemented by this function is:
-- expression := <term> <binary_op> <term>
-- expression := <term>
function Scan_Expression( this : not null access Expression_Parser'Class;
scanner : not null A_Token_Scanner;
precedence : Natural ) return A_Expression;
-- Scans an operand or returns null if one is not found. Parse_Exception
-- will be raised if a parsing error occurs. Token_Exception will be raised
-- if an unrecognized token is encountered by the scanner.
--
-- The grammar implemented by this function is:
-- operand := _number_
-- operand := _string_
-- operand := _identifier_
-- operand := '(' <expression> ')'
function Scan_Operand( this : not null access Expression_Parser'Class;
scanner : not null A_Token_Scanner ) return A_Expression;
-- Scans a term or returns null if one is not found. Parse_Exception will
-- be raised if a parsing error occurs. Token_Exception will be raised if an
-- unrecognized token is encountered by the scanner.
--
-- The grammar implemented by this function is:
-- term := <unary_op> <term>
-- term := <operand>
function Scan_Term( this : not null access Expression_Parser'Class;
scanner : not null A_Token_Scanner ) return A_Expression;
end Expressions.Parsers;