1. with Tokens.Scanners;                   use Tokens.Scanners; 
  2.  
  3. package Expressions.Parsers is 
  4.  
  5.     -- An Expression_Parser parses an Expression from a token scanner. Given a 
  6.     -- token scanner loaded with an input stream, the expression parser will 
  7.     -- read tokens from the scanner and create a corresponding Expression tree. 
  8.     type Expression_Parser is new Limited_Object with private; 
  9.     type A_Expression_Parser is access all Expression_Parser'Class; 
  10.  
  11.     -- Creates and returns a new Expression_Parser. 
  12.     function Create_Expression_Parser return A_Expression_Parser; 
  13.  
  14.     -- Scans an expression or raises Parse_Exception if one is not found. 
  15.     -- Parse_Exception will be raised if a parsing error occurs. Token_Exception 
  16.     -- will be raised if an unrecognized token is encountered by the scanner. 
  17.     function Expect_Expression( this    : not null access Expression_Parser'Class; 
  18.                                 scanner : not null A_Token_Scanner ) return A_Expression; 
  19.     pragma Postcondition( Expect_Expression'Result /= null ); 
  20.  
  21.     -- Scans an expression or returns null if one is not found. Parse_Exception 
  22.     -- will be raised if a parsing error occurs. Token_Exception will be raised 
  23.     -- if an unrecognized token is encountered by the scanner. 
  24.     function Scan_Expression( this    : not null access Expression_Parser'Class; 
  25.                               scanner : not null A_Token_Scanner ) return A_Expression; 
  26.  
  27.     -- Deletes the Expression_Parser. 
  28.     procedure Delete( this : in out A_Expression_Parser ); 
  29.  
  30. private 
  31.  
  32.     type Expression_Parser is new Limited_Object with null record; 
  33.  
  34.     -- Scans an expression or raises Parse_Exception if one is not found. If a 
  35.     -- boolean operator is encountered, it must have an operator precedence 
  36.     -- greater than or equal to 'precedence'. Parse_Exception will be raised if 
  37.     -- a parsing error occurs. Token_Exception will be raised if an unrecognized 
  38.     -- token is encountered by the scanner. 
  39.     -- 
  40.     -- The grammar implemented by this function is: 
  41.     -- expression := <term> <binary_op> <term> 
  42.     -- expression := <term> 
  43.     function Expect_Expression( this       : not null access Expression_Parser'Class; 
  44.                                 scanner    : not null A_Token_Scanner; 
  45.                                 precedence : Natural ) return A_Expression; 
  46.     pragma Postcondition( Expect_Expression'Result /= null ); 
  47.  
  48.     -- Scans an expression or returns null if one is not found. If a boolean 
  49.     -- operator is encountered, it must have an operator precedence greater than 
  50.     -- or equal to 'precedence'. Parse_Exception will be raised if a parsing 
  51.     -- error occurs. Token_Exception will be raised if an unrecognized token is 
  52.     -- encountered by the scanner. 
  53.     -- 
  54.     -- The grammar implemented by this function is: 
  55.     -- expression := <term> <binary_op> <term> 
  56.     -- expression := <term> 
  57.     function Scan_Expression( this       : not null access Expression_Parser'Class; 
  58.                               scanner    : not null A_Token_Scanner; 
  59.                               precedence : Natural ) return A_Expression; 
  60.  
  61.     -- Scans an operand or returns null if one is not found. Parse_Exception 
  62.     -- will be raised if a parsing error occurs. Token_Exception will be raised 
  63.     -- if an unrecognized token is encountered by the scanner. 
  64.     -- 
  65.     -- The grammar implemented by this function is: 
  66.     -- operand := _number_ 
  67.     -- operand := _string_ 
  68.     -- operand := _identifier_ 
  69.     -- operand := '(' <expression> ')' 
  70.     function Scan_Operand( this    : not null access Expression_Parser'Class; 
  71.                            scanner : not null A_Token_Scanner ) return A_Expression; 
  72.  
  73.     -- Scans a term or returns null if one is not found. Parse_Exception will 
  74.     -- be raised if a parsing error occurs. Token_Exception will be raised if an 
  75.     -- unrecognized token is encountered by the scanner. 
  76.     -- 
  77.     -- The grammar implemented by this function is: 
  78.     -- term := <unary_op> <term> 
  79.     -- term := <operand> 
  80.     function Scan_Term( this    : not null access Expression_Parser'Class; 
  81.                         scanner : not null A_Token_Scanner ) return A_Expression; 
  82.  
  83. end Expressions.Parsers;