Calculator  Step 5
Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
parser Class Reference

#include <parse.hpp>

Public Types

enum  kind : int {
  eof, identifier, number, string,
  plus ='+', minus ='-', times ='*', slash ='/',
  lparen = '(', rparen =')', equal ='=', comma =','
}
 

Public Member Functions

 parser (std::istream &input)
 
bool get_statement (std::ostream &output)
 

Private Member Functions

std::string charify (char c)
 
bool get_number (std::string const &token, node &result)
 
bool get_expr (node &result)
 
bool get_add_expr (node &result)
 
bool get_mul_expr (node &result)
 
bool get_primary (node &result)
 
bool get_unary (node &result)
 
void get_definition (std::string &name, identifier_list &parameters, node &definition)
 
kind get_token (std::string &token)
 
void get_identifier (std::string &identifier)
 
void get_expr_list (node_list &result)
 
template<class OutputIterator >
OutputIterator get_namelist (OutputIterator output)
 
void get_escape (std::string &str)
 
void get_string (std::string &result, char delimiter)
 
void push_back (std::string const &token, kind k)
 
bool isalpha (char c) const
 
bool isalnum (char c) const
 
bool isdigit (char c) const
 
bool isprint (char c) const
 

Private Attributes

std::istream & input_
 Share the input stream. More...
 
std::ctype< char > const & ctype_
 Cache the ctype facet for checking character categories. More...
 
std::string token_
 One token push-back. More...
 
kind kind_
 The kind of token that was pushed back. More...
 

Detailed Description

Parser class template. The parser reads tokens from an input stream. A token can be a keyword, numeric literal, identifier, or symbol (operator or punctuator). Symbols can have multiple characters (e.g., :=).

Because the recursive-descent parser can examine too many tokens from the input stream, it keeps a push-back token. Once the parser knows it has gone too far, it pushes back the most recently read token. The next call to get_token() retrieves the pushed-back token.

Only one push-back is available, which limits the complexity of the syntax.

Definition at line 25 of file parse.hpp.

Member Enumeration Documentation

enum parser::kind : int

Token kind. Declare a name for each single-character token, to ensure the enumerated type can represent any operator or punctuator character.

Enumerator
eof 
identifier 
number 
string 
plus 
minus 
times 
slash 
lparen 
rparen 
equal 
comma 

Definition at line 31 of file parse.hpp.

Constructor & Destructor Documentation

parser::parser ( std::istream &  input)

Constructor. Save the input stream.

Parameters
inputThe input stream

Definition at line 10 of file parse.cpp.

11 : input_(input),
12  ctype_(std::use_facet<std::ctype<char> >(input.getloc())),
13  token_(),
14  kind_()
15 {}
std::string token_
One token push-back.
Definition: parse.hpp:195
std::istream & input_
Share the input stream.
Definition: parse.hpp:193
kind kind_
The kind of token that was pushed back.
Definition: parse.hpp:196
std::ctype< char > const & ctype_
Cache the ctype facet for checking character categories.
Definition: parse.hpp:194

Member Function Documentation

std::string parser::charify ( char  c)
private

Convert a characer to a readable form.

Parameters
cThe character
Returns
A C++-style character literal that ensures c is readable.

Definition at line 17 of file parse.cpp.

References isprint().

Referenced by get_identifier(), and get_token().

18 {
19  if (c == '\a') return R"('\a')";
20  if (c == '\b') return R"('\b')";
21  if (c == '\f') return R"('\f')";
22  if (c == '\n') return R"('\n')";
23  if (c == '\r') return R"('\r')";
24  if (c == '\t') return R"('\t')";
25  if (c == '\v') return R"('\v')";
26  if (c == '\'') return R"('\'')";
27  if (c == '\\') return R"('\\')";
28 
29  if (isprint(c))
30  return std::string{"\'"} + std::string(1,c) + "\'";
31  else {
32  std::ostringstream stream{};
33  stream << "'\\x" << std::hex;
34  stream.fill('0');
35  stream.width(2);
36  stream << (std::char_traits<char>::to_int_type(c) & 0xFF) << '\'';
37  return stream.str();
38  }
39 }
bool isprint(char c) const
Definition: parse.hpp:191
bool parser::get_add_expr ( node result)
private

Parse an addition expression

ADD_EXPR ::= MUL_EXPR | ADD_EXPR + MUL_EXPR | ADD_EXPR - MUL_EXPR
Parameters
resultStore the result here
Returns
true to continue parsing or false to stop (end of file or error)

Definition at line 296 of file parse.cpp.

References get_mul_expr(), get_token(), and push_back().

Referenced by get_expr().

297 {
298  if (not get_mul_expr(result))
299  return false;
300  std::string token;
301  while (kind k = get_token(token)) {
302  if (k != '+' and k != '-') {
303  push_back(token, k);
304  return true;
305  } else {
306  node right;
307  if (not get_mul_expr(right))
308  throw syntax_error{"unterminated expression. Expected a multiplicative-expression after " + token};
309  result = node(result, k, right);
310  }
311  }
312  return true;
313 }
Definition: node.hpp:26
void push_back(std::string const &token, kind k)
Definition: parse.cpp:121
kind get_token(std::string &token)
Definition: parse.cpp:130
kind
Definition: parse.hpp:31
bool get_mul_expr(node &result)
Definition: parse.cpp:315
void parser::get_definition ( std::string &  name,
identifier_list parameters,
node definition 
)
private

Parse a function or variable definition A variable is just like a function that takes no parameters.

DEFINITION ::= DEF IDENTIFIER OPT_PARAMETERS '=' EXPR
OPT_PARAMETERS ::= emtpy | '(' OPT_IDENTIFIER_LIST ')'
OPT_IDENTIFIER_LIST ::=  empty | IDENTIFIER_LIST
IDENTIFIER_LIST ::= IDENTIFIER | IDENTIFIER_LIST ',' IDENTIFIER
Parameters
[out]nameStore the variable or function name here
[out]parametersStore the list of parameter names here
[out]definitionStore the definition expression here

Definition at line 225 of file parse.cpp.

References get_expr(), get_namelist(), get_token(), and identifier.

Referenced by get_statement().

226 {
227  // Define a variable.
228  kind k = get_token(name);
229  if (k != identifier)
230  throw syntax_error("expected IDENTIFIER, got " + name);
231 
232  std::string token;
233  k = get_token(token);
234  if (k == '(') {
235  get_namelist(std::back_inserter(parameters));
236  k = get_token(token);
237  }
238 
239  if (k != '=')
240  throw syntax_error("expected = in definition, got " + token);
241 
242  if (not get_expr(definition))
243  throw syntax_error("expected exprssion in assignment");
244 }
bool get_expr(node &result)
Definition: parse.cpp:291
OutputIterator get_namelist(OutputIterator output)
Definition: parse.hpp:207
kind get_token(std::string &token)
Definition: parse.cpp:130
kind
Definition: parse.hpp:31
void parser::get_escape ( std::string &  str)
private

Interpret a backslash escape sequence. The caller must have read the backslash already.

Parameters
[out]strWrite the string equivalent of the escape sequence at the end of this string

Definition at line 41 of file parse.cpp.

References ctype_, and input_.

Referenced by get_string().

42 {
43  char c{};
44  if (not input_.get(c))
45  throw syntax_error("incomplete escape");
46  if (c == '\n')
47  return;
48 
49  if (c == 'a')
50  str += '\a';
51  else if (c == 'b')
52  str += '\b';
53  else if (c == 'n')
54  str += '\n';
55  else if (c == 'f')
56  str += '\f';
57  else if (c == 'r')
58  str += '\r';
59  else if (c == 't')
60  str += '\t';
61  else if (c == 'v')
62  str += '\v';
63  else if (c == 'x') {
64  // hexadecimal sequence
65  std::string digits{};
66  while(input_.get(c) and ctype_.is(ctype_.xdigit, c))
67  digits += c;
68  if (input_)
69  input_.unget();
70  unsigned long value{std::stoul(digits, nullptr, 16)};
71  str += static_cast<char>(static_cast<unsigned char>(value));
72  } else if (c >= '0' and c <= '7') {
73  // octal sequence
74  std::string digits{};
75  for (int i = 0; i < 3 and input_.get(c) and c >= '0' and c <= '7'; ++i)
76  digits += c;
77  if (input_ or c < '0' or c > '7')
78  input_.unget();
79  unsigned long value{std::stoul(digits, nullptr, 8)};
80  str += static_cast<char>(static_cast<unsigned char>(value));
81  } else {
82  str += c;
83  }
84 }
std::istream & input_
Share the input stream.
Definition: parse.hpp:193
std::ctype< char > const & ctype_
Cache the ctype facet for checking character categories.
Definition: parse.hpp:194
bool parser::get_expr ( node result)
private

Parse an expression

Parameters
resultStore the result here
Returns
true to continue parsing or false to stop (end of file or error)

Definition at line 291 of file parse.cpp.

References get_add_expr().

Referenced by get_definition(), get_expr_list(), get_primary(), and get_statement().

292 {
293  return get_add_expr(result);
294 }
bool get_add_expr(node &result)
Definition: parse.cpp:296
void parser::get_expr_list ( node_list result)
private

Parse a comma-separated expression list.

Parameters
[out]resultStore the result here

Definition at line 355 of file parse.cpp.

References get_expr(), get_token(), and push_back().

Referenced by get_primary().

356 {
357  result.clear();
358  std::string token{};
359  while (kind k = get_token(token)) {
360  if (k == ')')
361  return;
362  push_back(token, k);
363  node expr{};
364  if (not get_expr(expr))
365  throw syntax_error{"unexpected end of line in function argument"};
366  result.push_back(expr);
367  k = get_token(token);
368  if (k == ')')
369  return;
370  else if (k != ',')
371  throw syntax_error{"expected comma in argument list, got " + token};
372  }
373  throw syntax_error{"unexpected end of line in function argument list"};
374 }
bool get_expr(node &result)
Definition: parse.cpp:291
Definition: node.hpp:26
void push_back(std::string const &token, kind k)
Definition: parse.cpp:121
kind get_token(std::string &token)
Definition: parse.cpp:130
kind
Definition: parse.hpp:31
void parser::get_identifier ( std::string &  identifier)
private

Parse an identifer.

Parameters
identifierStore the identifier here.
Precondition
first input character is alphabetic

Definition at line 102 of file parse.cpp.

References charify(), input_, isalnum(), and isalpha().

Referenced by get_token().

103 {
104  identifier.clear();
105  char c;
106  if (not input_.get(c))
107  return;
108  if (not isalpha(c))
109  throw syntax_error("expected alphabetic, got " + charify(c));
110  identifier += c;
111  while (input_.get(c)) {
112  if (not isalnum(c)) {
113  input_.unget();
114  return;
115  }
116  identifier += c;
117  }
118  return;
119 }
std::string charify(char c)
Definition: parse.cpp:17
bool isalpha(char c) const
Definition: parse.hpp:176
std::istream & input_
Share the input stream.
Definition: parse.hpp:193
bool isalnum(char c) const
Definition: parse.hpp:181
bool parser::get_mul_expr ( node result)
private

Parse a multiplicative expression.

MUL_EXPR ::= UNARY | MUL_EXPR + UNARY | MUL_EXPR - UNARY
Parameters
resultStore the result here
Returns
true to continue parsing or false to stop (end of file or error)

Definition at line 315 of file parse.cpp.

References get_token(), get_unary(), and push_back().

Referenced by get_add_expr().

316 {
317  if (not get_unary(result))
318  return false;
319  std::string token{};
320  while (kind k = get_token(token)) {
321  if (k != '*' and k != '/') {
322  push_back(token, k);
323  return true;
324  } else {
325  node right{};
326  if (not get_unary(right))
327  throw syntax_error{"unterminated expression. Expected a unary-expression after " + token};
328  result = node(result, k, right);
329  }
330  }
331  return true;
332 }
Definition: node.hpp:26
void push_back(std::string const &token, kind k)
Definition: parse.cpp:121
bool get_unary(node &result)
Definition: parse.cpp:334
kind get_token(std::string &token)
Definition: parse.cpp:130
kind
Definition: parse.hpp:31
template<class OutputIterator >
OutputIterator parser::get_namelist ( OutputIterator  output)
private

Parse a list of parameter names. Names are identifiers, separated by commas. The list can be empty. This is a template so the container type is unimportant. Any output iterator will do.

Parameters
[out]outputStore the identifiers here
Returns
a copy of output after storing all the identifiers

Definition at line 207 of file parse.hpp.

References get_token(), and identifier.

Referenced by get_definition().

208 {
209  std::string token{};
210  while (kind k = get_token(token)) {
211  if (k == ')')
212  return output;
213  else if (k != identifier)
214  throw syntax_error{"expected function parameter, got " + token};
215  else {
216  *output = token;
217  ++output;
218 
219  k = get_token(token);
220  if (k == ')')
221  return output;
222  if (k != ',')
223  throw syntax_error{"expected comma in function paramter list, got " + token};
224  }
225  }
226  throw syntax_error{"unexpected end of line in function parameter list"};
227 }
kind get_token(std::string &token)
Definition: parse.cpp:130
kind
Definition: parse.hpp:31
bool parser::get_number ( std::string const &  token,
node result 
)
private

Parse a floating number.

Parameters
tokenThe token to parse
resultStore the number here
Returns
true if token is a valid number or false for an error

Definition at line 214 of file parse.cpp.

Referenced by get_primary().

215 {
216  std::istringstream stream(token);
217  // If the value overflows or is otherwise invalid, return false.
218  double value;
219  if (not (stream >> value))
220  return false;
221  result = node(value);
222  return true;
223 }
Definition: node.hpp:26
bool parser::get_primary ( node result)
private

Parse a primary expression.

PRIMARY ::= NUMBER | IDENTIFIER | '(' EXPR ')' | FUNCTION_CALL
FUNCTION_CALL ::= IDENTIFIER '(' OPT_EXPR_LIST ')'
OPT_EXPR_LIST ::= empty | EXPR_LIST
EXPR_LIST ::= EXPR | EXPR_LIST ',' EXPR
Parameters
resultStore the result here
Returns
true to continue parsing or false to stop (end of file or error)

Definition at line 376 of file parse.cpp.

References eof, get_expr(), get_expr_list(), get_number(), get_token(), identifier, number, and push_back().

Referenced by get_unary().

377 {
378  std::string token{};
379  kind k = get_token(token);
380  if (k == eof)
381  return false;
382 
383  if (k == '(') {
384  // Parenthesized expression
385  if (not get_expr(result))
386  throw syntax_error{"expected expression, got end of line"};
387  k = get_token(token);
388  if (k != ')')
389  throw syntax_error{"expected ')', got " + token};
390  else
391  return true;
392  }
393 
394  if (k == number) {
395  // Numeric literal
396  if (not get_number(token, result))
397  throw syntax_error{"Invalid numeric literal: " + token};
398  return true;
399  }
400 
401  if (k == identifier) {
402  // Identifier: variable or function call
403  std::string next{};
404  k = get_token(next);
405  if (k == '(') {
406  // function call
407  node_list arguments{};
408  get_expr_list(arguments);
409  result = node{std::move(token), std::move(arguments)};
410  } else {
411  static const node_list no_arguments;
412  // Variable reference or function call with no arguments
413  push_back(next, k);
414  result = node{std::move(token), no_arguments};
415  }
416  return true;
417  }
418  throw syntax_error{"expected a primary, got " + token};
419 }
bool get_expr(node &result)
Definition: parse.cpp:291
Definition: node.hpp:26
void push_back(std::string const &token, kind k)
Definition: parse.cpp:121
kind get_token(std::string &token)
Definition: parse.cpp:130
kind
Definition: parse.hpp:31
std::vector< node > node_list
A sequence of nodes.
Definition: node.hpp:13
bool get_number(std::string const &token, node &result)
Definition: parse.cpp:214
void get_expr_list(node_list &result)
Definition: parse.cpp:355
bool parser::get_statement ( std::ostream &  output)

Read one statement and store the parse tree in result. If the statement is an assignment or function definition, store the variable or function. If the statement is an expression, print the result to output.

STATEMENT ::= DEFINITION | QUIT | EXPR | SAVE | LOAD
SAVE ::= "save" FILENAME
LOAD ::= "load" FILENAME
FILENAME ::= quoted-string
Parameters
outputThe output stream.
Returns
true to continue or false to end the loop
Exceptions
parse_errorfor various syntax and other errors

Definition at line 246 of file parse.cpp.

References eof, node::evaluate(), get_definition(), get_expr(), get_token(), identifier, load_library(), push_back(), save_library(), and set_function().

Referenced by parse_loop().

247 {
248  std::string token;
249  kind k(get_token(token));
250  if (k == eof)
251  return false;
252 
253  if (k == identifier and token == "def") {
254  node definition;
255  identifier_list parameters;
256  get_definition(token, parameters, definition);
257  set_function(token, node(parameters, definition));
258  return true;
259  }
260 
261  if (k == identifier and token == "quit")
262  std::exit(0);
263 
264  if (k == identifier and token == "save") {
265  std::string filename;
266  if (get_token(filename) != string)
267  throw syntax_error("expected FILENAME after save, got " + token);
268  save_library(filename);
269  output << "Library saved to " << filename << '\n';
270  }
271 
272  if (k == identifier and token == "load") {
273  std::string filename;
274  if (get_token(filename) != string)
275  throw syntax_error("expected FILENAME after load, got " + token);
276  load_library(filename);
277  output << "Library loaded from " << filename << '\n';
278  }
279  // Otherwise, the statement must be an expression.
280  push_back(token, k);
281  node n;
282  if (not get_expr(n))
283  return false;
284  else {
285  // Evaluate the expression and print the result.
286  output << n.evaluate() << '\n';
287  return true;
288  }
289 }
void load_library(std::string const &filename)
Definition: variables.cpp:100
bool get_expr(node &result)
Definition: parse.cpp:291
void save_library(std::string const &filename)
Definition: variables.cpp:79
Definition: node.hpp:26
void get_definition(std::string &name, identifier_list &parameters, node &definition)
Definition: parse.cpp:225
void push_back(std::string const &token, kind k)
Definition: parse.cpp:121
std::vector< std::string > identifier_list
A sequence of identifiers (e.g., parameter names).
Definition: node.hpp:19
kind get_token(std::string &token)
Definition: parse.cpp:130
void set_function(std::string const &name, node value)
Definition: variables.cpp:74
double evaluate() const
Definition: node.cpp:60
kind
Definition: parse.hpp:31
void parser::get_string ( std::string &  result,
char  delimiter 
)
private

Parse a quoted string. The caller passes the quote character in the delimiter argument.

Parameters
[out]resultStore the token here.
[in]delimiterThe quote character (' or ")

Definition at line 86 of file parse.cpp.

References get_escape(), and input_.

Referenced by get_token().

87 {
88  char c{};
89  while (input_.get(c)) {
90  if (c == delimiter)
91  return;
92  else if (c == '\\')
93  get_escape(result);
94  else if (c == '\n')
95  throw syntax_error("unterminated string");
96  else
97  result += c;
98  }
99  throw syntax_error("unterminated string");
100 }
std::istream & input_
Share the input stream.
Definition: parse.hpp:193
void get_escape(std::string &str)
Definition: parse.cpp:41
parser::kind parser::get_token ( std::string &  token)
private

Parse a token. A token can be a keyword, a literal or a symbol.

TOKEN ::= IDENTIFIER | NUMBER | SYMBOL | STRING
IDENTIIFER ::= ALPHA (ALPHA | DIGIT)*
NUMBER ::= DIGIT+ ('.' DIGITS+)? ('E' SIGN? DIGITS+)?
SYMBOL ::= '+' | '-' | '*' | '/' | '%' | '(' | ')' | '=' | ','
Parameters
tokenStore the text of the token here.
Returns
the token kind

Definition at line 130 of file parse.cpp.

References charify(), eof, get_identifier(), get_string(), identifier, input_, isalpha(), kind_, number, string, and token_.

Referenced by get_add_expr(), get_definition(), get_expr_list(), get_mul_expr(), get_namelist(), get_primary(), get_statement(), and get_unary().

131 {
132  if (not token_.empty())
133  {
134  kind result(kind_);
135  token = token_;
136 
137  token_.clear();
138  kind_ = eof;
139 
140  return result;
141  }
142 
143  char c;
144  if (not (input_ >> c)) {
145  token = "end of line";
146  return eof;
147  }
148  if (isalpha(c)) {
149  input_.unget();
150  get_identifier(token);
151  return identifier;
152  }
153 
154  if (c == '\'' or c == '"') {
155  // Quoted string
156  token.clear();
157  get_string(token, c);
158  return string;
159  }
160 
161  // Get a numeric literal.
162  token.clear();
163  if (c == '+' or c == '-' or c == '*' or c == '/' or c == '%' or c == '(' or c == ')' or c == '=' or c == ',') {
164  token += c;
165  return kind(c);
166  }
167 
168  if (c < '0' or c > '9') {
169  input_.unget();
170  throw syntax_error("expected digit, got " + charify(c));
171  }
172  while (c >= '0' and c <= '9') {
173  token += c;
174  if (not input_.get(c))
175  return number;
176  }
177  if (c == '.') {
178  token += c;
179  if (not input_.get(c))
180  throw syntax_error("unterminated number: expected digit after the decimal point");
181  if (c < '0' or c > '9') {
182  input_.unget();
183  throw syntax_error("expected digit after decimal point, got " + charify(c));
184  }
185  while (c >= '0' and c <= '9') {
186  token += c;
187  if (not input_.get(c))
188  return number;
189  }
190  }
191  if (c == 'e' or c == 'E') {
192  token += c;
193  if (not input_.get(c))
194  throw syntax_error("unterminated number: expected digit in the exponent");
195  if (c == '-' or c == '+') {
196  token += c;
197  if (not input_.get(c))
198  throw syntax_error("unterminated number: expected digit after sign in the exponent");
199  }
200  if (c < '0' or c > '9') {
201  input_.unget();
202  throw syntax_error("expected digit in the exponent, got " + charify(c));
203  }
204  while (c >= '0' and c <= '9') {
205  token += c;
206  if (not input_.get(c))
207  return number;
208  }
209  }
210  input_.unget();
211  return number;
212 }
void get_string(std::string &result, char delimiter)
Definition: parse.cpp:86
void get_identifier(std::string &identifier)
Definition: parse.cpp:102
std::string charify(char c)
Definition: parse.cpp:17
bool isalpha(char c) const
Definition: parse.hpp:176
std::string token_
One token push-back.
Definition: parse.hpp:195
std::istream & input_
Share the input stream.
Definition: parse.hpp:193
kind kind_
The kind of token that was pushed back.
Definition: parse.hpp:196
kind
Definition: parse.hpp:31
bool parser::get_unary ( node result)
private

Parse a unary expression.

UNARY ::= '-' PRIMARY | '+' PRIMARY | PRIMARY
Parameters
resultStore the result here
Returns
true to continue parsing or false to stop (end of file or error)

Definition at line 334 of file parse.cpp.

References eof, get_primary(), get_token(), and push_back().

Referenced by get_mul_expr().

335 {
336  std::string token{};
337  kind k = get_token(token);
338  if (k == eof)
339  return false;
340  if (k == '-') {
341  if (not get_primary(result))
342  throw syntax_error{"expected primary after unary " + token + ", got end of line"};
343  result = node(k, result);
344  return true;
345  } else if (k == '+') {
346  if (not get_primary(result))
347  throw syntax_error{"expected primary after unary +, got end of line"};
348  return true;
349  } else {
350  push_back(token, k);
351  return get_primary(result);
352  }
353 }
Definition: node.hpp:26
void push_back(std::string const &token, kind k)
Definition: parse.cpp:121
bool get_primary(node &result)
Definition: parse.cpp:376
kind get_token(std::string &token)
Definition: parse.cpp:130
kind
Definition: parse.hpp:31
bool parser::isalnum ( char  c) const
inlineprivate

Return true if c is alphanumeric. Use the locale of the input stream.

Parameters
cThe character to test.

Definition at line 181 of file parse.hpp.

References ctype_.

Referenced by get_identifier().

181 { return ctype_.is(ctype_.alnum, c); }
std::ctype< char > const & ctype_
Cache the ctype facet for checking character categories.
Definition: parse.hpp:194
bool parser::isalpha ( char  c) const
inlineprivate

Return true if c is alphabetic. Use the locale of the input stream.

Parameters
cThe character to test.

Definition at line 176 of file parse.hpp.

References ctype_.

Referenced by get_identifier(), and get_token().

176 { return ctype_.is(ctype_.alpha, c); }
std::ctype< char > const & ctype_
Cache the ctype facet for checking character categories.
Definition: parse.hpp:194
bool parser::isdigit ( char  c) const
inlineprivate

Return true if c is a digit. Use the locale of the input stream.

Parameters
cThe character to test.

Definition at line 186 of file parse.hpp.

References ctype_.

186 { return ctype_.is(ctype_.digit, c); }
std::ctype< char > const & ctype_
Cache the ctype facet for checking character categories.
Definition: parse.hpp:194
bool parser::isprint ( char  c) const
inlineprivate

Return true if c is printable. Use the locale of the input stream.

Parameters
cThe character to test.

Definition at line 191 of file parse.hpp.

References ctype_.

Referenced by charify().

191 { return ctype_.is(ctype_.print, c); }
std::ctype< char > const & ctype_
Cache the ctype facet for checking character categories.
Definition: parse.hpp:194
void parser::push_back ( std::string const &  token,
kind  k 
)
private

Push back a token. The next call to get_token() will return the pushed-back token.

Parameters
tokenThe token to push back.
kThe kind of token being pushed back

Definition at line 121 of file parse.cpp.

References eof, kind_, and token_.

Referenced by get_add_expr(), get_expr_list(), get_mul_expr(), get_primary(), get_statement(), and get_unary().

122 {
123  kind_ = k;
124  if (kind_ == eof)
125  token_ = "end of line";
126  else
127  token_ = token;
128 }
std::string token_
One token push-back.
Definition: parse.hpp:195
kind kind_
The kind of token that was pushed back.
Definition: parse.hpp:196

Member Data Documentation

std::ctype<char> const& parser::ctype_
private

Cache the ctype facet for checking character categories.

Definition at line 194 of file parse.hpp.

Referenced by get_escape(), isalnum(), isalpha(), isdigit(), and isprint().

std::istream& parser::input_
private

Share the input stream.

Definition at line 193 of file parse.hpp.

Referenced by get_escape(), get_identifier(), get_string(), and get_token().

kind parser::kind_
private

The kind of token that was pushed back.

Definition at line 196 of file parse.hpp.

Referenced by get_token(), and push_back().

std::string parser::token_
private

One token push-back.

Definition at line 195 of file parse.hpp.

Referenced by get_token(), and push_back().


The documentation for this class was generated from the following files: