1 // Copyright 2011 Google Inc. All Rights Reserved.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 #ifndef NINJA_LEXER_H_
16 #define NINJA_LEXER_H_
18 #include "string_piece.h"
20 // Windows may #define ERROR.
29 /// Helper ctor useful for tests.
30 explicit Lexer(const char* input);
50 /// Return a human-readable form of a token, used in error messages.
51 static const char* TokenName(Token t);
53 /// Return a human-readable token hint, used in error messages.
54 static const char* TokenErrorHint(Token expected);
56 /// If the last token read was an ERROR token, provide more info
57 /// or the empty string.
58 string DescribeLastError();
60 /// Start parsing some input.
61 void Start(StringPiece filename, StringPiece input);
63 /// Read a Token from the Token enum.
66 /// Rewind to the last read Token.
69 /// If the next token is \a token, read it and return true.
70 bool PeekToken(Token token);
72 /// Read a simple identifier (a rule or variable name).
73 /// Returns false if a name can't be read.
74 bool ReadIdent(string* out);
76 /// Read a path (complete with $escapes).
77 /// Returns false only on error, returned path may be empty if a delimiter
78 /// (space, newline) is hit.
79 bool ReadPath(EvalString* path, string* err) {
80 return ReadEvalString(path, true, err);
83 /// Read the value side of a var = value line (complete with $escapes).
84 /// Returns false only on error.
85 bool ReadVarValue(EvalString* value, string* err) {
86 return ReadEvalString(value, false, err);
89 /// Construct an error message with context.
90 bool Error(const string& message, string* err);
93 /// Skip past whitespace (called after each read token/ident/etc.).
96 /// Read a $-escaped string.
97 bool ReadEvalString(EvalString* eval, bool path, string* err);
99 StringPiece filename_;
102 const char* last_token_;
105 #endif // NINJA_LEXER_H_