12 /// identifier_expr: identifier | identifier '(' expression* ')'
13 ex parser::parse_identifier_expr()
15 std::string name = scanner->str;
16 get_next_tok(); // eat identifier.
18 if (token != '(') // symbol
19 return find_or_insert_symbol(name, syms, strict);
21 // function/ctor call.
22 get_next_tok(); // eat (
26 ex e = parse_expression();
33 Parse_error("expected ')' or ',' in argument list");
40 prototype the_prototype = make_pair(name, args.size());
41 prototype_table::const_iterator reader = funcs.find(the_prototype);
42 if (reader == funcs.end()) {
43 Parse_error_("no function \"" << name << "\" with " <<
44 args.size() << " arguments");
46 ex ret = reader->second(args);
50 /// paren_expr: '(' expression ')'
51 ex parser::parse_paren_expr()
53 get_next_tok(); // eat (.
54 ex e = parse_expression();
57 Parse_error("expected ')'");
58 get_next_tok(); // eat ).
62 extern numeric* _num_1_p;
65 /// unary_expr: [+-] expression
66 ex parser::parse_unary_expr()
68 // Unlike most other parse_* method this one does NOT consume
69 // current token so parse_binop_rhs() knows what kind of operator
72 // There are different kinds of expressions which need to be handled:
77 // Delegete the work to parse_binop_rhs(), otherwise we end up
78 // duplicating it here.
79 ex lhs = _ex0; // silly trick
80 ex e = parse_binop_rhs(0, lhs);
84 /// primary: identifier_expr | number_expr | paren_expr | unary_expr
85 ex parser::parse_primary()
88 case lexer::token_type::identifier:
89 return parse_identifier_expr();
90 case lexer::token_type::number:
91 return parse_number_expr();
93 return parse_paren_expr();
96 return parse_unary_expr();
97 case lexer::token_type::literal:
98 return parse_literal_expr();
99 case lexer::token_type::eof:
101 Parse_error("unexpected token");
105 /// expression ::= primary binoprhs
106 ex parser::parse_expression()
108 ex lhs = parse_primary();
109 ex res = parse_binop_rhs(0, lhs);
113 /// number_expr: number
114 ex parser::parse_number_expr()
116 ex n = numeric(scanner->str.c_str());
117 get_next_tok(); // consume the number
121 /// literal_expr: 'I' | 'Pi' | 'Euler' | 'Catalan'
122 ex parser::parse_literal_expr()
124 get_next_tok(); // consume the literal
125 if (scanner->str == "I")
127 else if (scanner->str == "Pi")
129 else if (scanner->str == "Euler")
131 else if (scanner->str == "Catalan")
133 bug("unknown literal: \"" << scanner->str << "\"");
136 ex parser::operator()(std::istream& input)
138 scanner->switch_input(&input);
140 ex ret = parse_expression();
141 // parse_expression() stops if it encounters an unknown token.
142 // This is not a bug: since the parser is recursive checking
143 // whether the next token is valid is responsibility of the caller.
144 // Hence make sure nothing is left in the stream:
145 if (token != lexer::token_type::eof)
146 Parse_error("expected EOF");
151 ex parser::operator()(const std::string& input)
153 std::istringstream is(input);
154 ex ret = operator()(is);
158 int parser::get_next_tok()
160 token = scanner->gettok();
164 parser::parser(const symtab& syms_, const bool strict_,
165 const prototype_table& funcs_) : strict(strict_),
166 funcs(funcs_), syms(syms_)
168 scanner = new lexer();