|
Ninja
|
#include <lexer.h>
Public Types | |
| enum | Token { ERROR , BUILD , COLON , DEFAULT , EQUALS , IDENT , INCLUDE , INDENT , NEWLINE , PIPE , PIPE2 , PIPEAT , POOL , RULE , SUBNINJA , TEOF } |
Public Member Functions | |
| std::string | DescribeLastError () |
| If the last token read was an ERROR token, provide more info or the empty string. | |
| bool | Error (const std::string &message, std::string *err) |
| Construct an error message with context. | |
| Lexer () | |
| Lexer (const char *input) | |
| Helper ctor useful for tests. | |
| bool | PeekToken (Token token) |
| If the next token is token, read it and return true. | |
| bool | ReadIdent (std::string *out) |
| Read a simple identifier (a rule or variable name). | |
| bool | ReadPath (EvalString *path, std::string *err) |
| Read a path (complete with $escapes). | |
| Token | ReadToken () |
| Read a Token from the Token enum. | |
| bool | ReadVarValue (EvalString *value, std::string *err) |
| Read the value side of a var = value line (complete with $escapes). | |
| void | Start (StringPiece filename, StringPiece input) |
| Start parsing some input. | |
| void | UnreadToken () |
| Rewind to the last read Token. | |
Static Public Member Functions | |
| static const char * | TokenErrorHint (Token expected) |
| Return a human-readable token hint, used in error messages. | |
| static const char * | TokenName (Token t) |
| Return a human-readable form of a token, used in error messages. | |
Private Member Functions | |
| void | EatWhitespace () |
| Skip past whitespace (called after each read token/ident/etc.). | |
| bool | ReadEvalString (EvalString *eval, bool path, std::string *err) |
| Read a $-escaped string. | |
Private Attributes | |
| StringPiece | filename_ |
| StringPiece | input_ |
| const char * | last_token_ |
| const char * | ofs_ |
| enum Lexer::Token |
|
explicit |
| string Lexer::DescribeLastError | ( | ) |
If the last token read was an ERROR token, provide more info or the empty string.
Definition at line 106 of file lexer.cc.
References last_token_.
Referenced by ReadEvalString(), and TEST().
|
private |
Skip past whitespace (called after each read token/ident/etc.).
re2c [ ]+ { continue; } "$\r\n" { continue; } "$\n" { continue; } nul { break; } [^] { break; }
Definition at line 471 of file lexer.cc.
References ofs_.
Referenced by ReadEvalString(), ReadIdent(), and ReadToken().
| bool Lexer::Error | ( | const std::string & | message, |
| std::string * | err ) |
Construct an error message with context.
Definition at line 25 of file lexer.cc.
References filename_, input_, and last_token_.
Referenced by Parser::Load(), and ReadEvalString().
| bool Lexer::PeekToken | ( | Token | token | ) |
If the next token is token, read it and return true.
Definition at line 463 of file lexer.cc.
References ReadToken(), and UnreadToken().
|
private |
Read a $-escaped string.
re2c [^$ :\r
|\000]+ { eval->AddText(StringPiece(start, p - start)); continue; } "\r\n" { if (path) p = start; break; } [ :|
] { if (path) { p = start; break; } else { if (*start == '
') break; eval->AddText(StringPiece(start, 1)); continue; } } "$$" { eval->AddText(StringPiece("$", 1)); continue; } "$ " { eval->AddText(StringPiece(" ", 1)); continue; } "$\r\n"[ ]* { continue; } "$\n"[ ]* { continue; } "${"varname"}" { eval->AddSpecial(StringPiece(start + 2, p - start - 3)); continue; } "$"simple_varname { eval->AddSpecial(StringPiece(start + 1, p - start - 1)); continue; } "$:" { eval->AddText(StringPiece(":", 1)); continue; } "$". { last_token_ = start; return Error("bad $-escape (literal $ must be written as $$)", err); } nul { last_token_ = start; return Error("unexpected EOF", err); } [^] { last_token_ = start; return Error(DescribeLastError(), err); }
Definition at line 623 of file lexer.cc.
References EvalString::AddSpecial(), EvalString::AddText(), DescribeLastError(), EatWhitespace(), Error(), last_token_, and ofs_.
Referenced by ReadPath(), and ReadVarValue().
| bool Lexer::ReadIdent | ( | std::string * | out | ) |
Read a simple identifier (a rule or variable name).
Returns false if a name can't be read.
re2c varname { out->assign(start, p - start); break; } [^] { last_token_ = start; return false; }
Definition at line 554 of file lexer.cc.
References EatWhitespace(), last_token_, and ofs_.
|
inline |
Read a path (complete with $escapes).
Returns false only on error, returned path may be empty if a delimiter (space, newline) is hit.
Definition at line 80 of file lexer.h.
References ReadEvalString().
| Lexer::Token Lexer::ReadToken | ( | ) |
Read a Token from the Token enum.
re2c re2c:define:YYCTYPE = "unsigned char"; re2c:define:YYCURSOR = p; re2c:define:YYMARKER = q; re2c:yyfill:enable = 0;
nul = "\000"; simple_varname = [a-zA-Z0-9_-]+; varname = [a-zA-Z0-9_.-]+;
[ ]*"#"[^\000
]*"\n" { continue; } [ ]*"\r\n" { token = NEWLINE; break; } [ ]*"\n" { token = NEWLINE; break; } [ ]+ { token = INDENT; break; } "build" { token = BUILD; break; } "pool" { token = POOL; break; } "rule" { token = RULE; break; } "default" { token = DEFAULT; break; } "=" { token = EQUALS; break; } ":" { token = COLON; break; } "|@" { token = PIPEAT; break; } "||" { token = PIPE2; break; } "|" { token = PIPE; break; } "include" { token = INCLUDE; break; } "subninja" { token = SUBNINJA; break; } varname { token = IDENT; break; } nul { token = TEOF; break; } [^] { token = ERROR; break; }
Definition at line 120 of file lexer.cc.
References BUILD, COLON, DEFAULT, EatWhitespace(), EQUALS, ERROR, IDENT, INCLUDE, INDENT, last_token_, NEWLINE, ofs_, PIPE, PIPE2, PIPEAT, POOL, RULE, SUBNINJA, and TEOF.
Referenced by PeekToken(), TEST(), and TEST().
|
inline |
| void Lexer::Start | ( | StringPiece | filename, |
| StringPiece | input ) |
|
static |
Return a human-readable token hint, used in error messages.
Definition at line 97 of file lexer.cc.
References COLON.
Referenced by Parser::ExpectToken().
|
static |
Return a human-readable form of a token, used in error messages.
Definition at line 75 of file lexer.cc.
References BUILD, COLON, DEFAULT, EQUALS, ERROR, IDENT, INCLUDE, INDENT, NEWLINE, PIPE, PIPE2, PIPEAT, POOL, RULE, SUBNINJA, and TEOF.
Referenced by Parser::ExpectToken(), DyndepParser::Parse(), and ManifestParser::Parse().
| void Lexer::UnreadToken | ( | ) |
Rewind to the last read Token.
Definition at line 116 of file lexer.cc.
References last_token_, and ofs_.
Referenced by PeekToken().
|
private |
|
private |
|
private |
Definition at line 103 of file lexer.h.
Referenced by DescribeLastError(), Error(), ReadEvalString(), ReadIdent(), ReadToken(), Start(), and UnreadToken().
|
private |
Definition at line 102 of file lexer.h.
Referenced by EatWhitespace(), ReadEvalString(), ReadIdent(), ReadToken(), Start(), and UnreadToken().