summaryrefslogtreecommitdiffstats
path: root/src/parsers.h
diff options
context:
space:
mode:
authorEvan Martin <martine@danga.com>2011-05-23 16:17:39 (GMT)
committerEvan Martin <martine@danga.com>2011-05-23 16:17:39 (GMT)
commit2bdd317515b574582467c8c284e4fdfa6c161f15 (patch)
treed7120928e692f4c6ba49c88f7d58bf08ad6ebf9d /src/parsers.h
parent91e0f9ff96cc688c48973696d7d1dd2f4e356e3e (diff)
downloadNinja-2bdd317515b574582467c8c284e4fdfa6c161f15.zip
Ninja-2bdd317515b574582467c8c284e4fdfa6c161f15.tar.gz
Ninja-2bdd317515b574582467c8c284e4fdfa6c161f15.tar.bz2
refactor parser, check in some failing tests
Diffstat (limited to 'src/parsers.h')
-rw-r--r--src/parsers.h22
1 files changed, 20 insertions, 2 deletions
diff --git a/src/parsers.h b/src/parsers.h
index 9dc61d9..55c0eb7 100644
--- a/src/parsers.h
+++ b/src/parsers.h
@@ -48,16 +48,30 @@ struct Token {
const char* end_;
};
+/// Represents a user-understandable position within a source file.
+struct SourceLocation {
+ SourceLocation(int line, int col) : line_(line), column_(col) {}
+
+ /// Construct an error message based on the position and message,
+ /// write it into \a err, then return false.
+ bool Error(const string& message, string* err);
+
+ /// 1-based line and column numbers.
+ int line_;
+ int column_;
+};
+
/// Processes an input stream into Tokens.
struct Tokenizer {
Tokenizer(bool whitespace_significant)
: whitespace_significant_(whitespace_significant),
- token_(Token::NONE), line_number_(1),
+ token_(Token::NONE), line_number_(0),
last_indent_(0), cur_indent_(-1) {}
void Start(const char* start, const char* end);
+ /// Report an error with a location pointing at the current token.
bool Error(const string& message, string* err);
- // Call Error() with "expected foo, got bar".
+ /// Call Error() with "expected foo, got bar".
bool ErrorExpected(const string& expected, string* err);
const Token& token() const { return token_; }
@@ -73,6 +87,10 @@ struct Tokenizer {
Token::Type PeekToken();
void ConsumeToken();
+ SourceLocation Location() {
+ return SourceLocation(line_number_ + 1, token_.pos_ - cur_line_ + 1);
+ }
+
bool whitespace_significant_;
const char* cur_;