summaryrefslogtreecommitdiff
path: root/base/test/expectations/parser.cc
blob: c7132e5d4844ae4315de494d1b52c3bca1fc4729 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/test/expectations/parser.h"

#include "base/strings/string_util.h"

namespace test_expectations {

Parser::Parser(Delegate* delegate, const std::string& input)
    : delegate_(delegate),
      input_(input),
      pos_(NULL),
      end_(NULL),
      line_number_(0),
      data_error_(false) {
}

Parser::~Parser() {
}

void Parser::Parse() {
  pos_ = &input_[0];
  end_ = pos_ + input_.length();

  line_number_ = 1;

  StateFuncPtr state = &Parser::Start;
  while (state) {
    state = (this->*state)();
  }
}

inline bool Parser::HasNext() {
  return pos_ < end_;
}

Parser::StateFunc Parser::Start() {
  // If at the start of a line is whitespace, skip it and arrange to come back
  // here.
  if (IsAsciiWhitespace(*pos_))
    return SkipWhitespaceAndNewLines(&Parser::Start);

  // Handle comments at the start of lines.
  if (*pos_ == '#')
    return &Parser::ParseComment;

  // After arranging to come back here from skipping whitespace and comments,
  // the parser may be at the end of the input.
  if (pos_ >= end_)
    return NULL;

  current_ = Expectation();
  data_error_ = false;

  return &Parser::ParseBugURL;
}

Parser::StateFunc Parser::ParseComment() {
  if (*pos_ != '#')
    return SyntaxError("Invalid start of comment");

  do {
    ++pos_;
  } while (HasNext() && *pos_ != '\n');

  return &Parser::Start;
}

Parser::StateFunc Parser::ParseBugURL() {
  return SkipWhitespace(ExtractString(
      &Parser::BeginModifiers));
}

Parser::StateFunc Parser::BeginModifiers() {
  if (*pos_ != '[' || !HasNext())
    return SyntaxError("Expected '[' for start of modifiers");

  ++pos_;
  return SkipWhitespace(&Parser::InModifiers);
}

Parser::StateFunc Parser::InModifiers() {
  if (*pos_ == ']')
    return &Parser::EndModifiers;

  return ExtractString(SkipWhitespace(
      &Parser::SaveModifier));
}

Parser::StateFunc Parser::SaveModifier() {
  if (extracted_string_.empty())
    return SyntaxError("Invalid modifier list");

  Configuration config;
  if (ConfigurationFromString(extracted_string_, &config)) {
    if (current_.configuration != CONFIGURATION_UNSPECIFIED)
      DataError("Cannot use more than one configuration modifier");
    else
      current_.configuration = config;
  } else {
    Platform platform;
    if (PlatformFromString(extracted_string_, &platform))
      current_.platforms.push_back(platform);
    else
      DataError("Invalid modifier string");
  }

  return SkipWhitespace(&Parser::InModifiers);
}

Parser::StateFunc Parser::EndModifiers() {
 if (*pos_ != ']' || !HasNext())
    return SyntaxError("Expected ']' for end of modifiers list");

  ++pos_;
  return SkipWhitespace(&Parser::ParseTestName);
}

Parser::StateFunc Parser::ParseTestName() {
  return ExtractString(&Parser::SaveTestName);
}

Parser::StateFunc Parser::SaveTestName() {
  if (extracted_string_.empty())
    return SyntaxError("Invalid test name");

  current_.test_name = extracted_string_.as_string();
  return SkipWhitespace(&Parser::ParseExpectation);
}

Parser::StateFunc Parser::ParseExpectation() {
  if (*pos_ != '=' || !HasNext())
    return SyntaxError("Expected '=' for expectation result");

  ++pos_;
  return SkipWhitespace(&Parser::ParseExpectationType);
}

Parser::StateFunc Parser::ParseExpectationType() {
  return ExtractString(&Parser::SaveExpectationType);
}

Parser::StateFunc Parser::SaveExpectationType() {
  if (!ResultFromString(extracted_string_, &current_.result))
    DataError("Unknown expectation type");

  return SkipWhitespace(&Parser::End);
}

Parser::StateFunc Parser::End() {
  if (!data_error_)
    delegate_->EmitExpectation(current_);

  if (HasNext())
    return SkipWhitespaceAndNewLines(&Parser::Start);

  return NULL;
}

Parser::StateFunc Parser::ExtractString(StateFunc success) {
  const char* start = pos_;
  while (!IsAsciiWhitespace(*pos_) && *pos_ != ']' && HasNext()) {
    ++pos_;
    if (*pos_ == '#') {
      return SyntaxError("Unexpected start of comment");
    }
  }
  extracted_string_ = base::StringPiece(start, pos_ - start);
  return success;
}

Parser::StateFunc Parser::SkipWhitespace(Parser::StateFunc next) {
  while ((*pos_ == ' ' || *pos_ == '\t') && HasNext()) {
    ++pos_;
  }
  return next;
}

Parser::StateFunc Parser::SkipWhitespaceAndNewLines(Parser::StateFunc next) {
  while (IsAsciiWhitespace(*pos_) && HasNext()) {
    if (*pos_ == '\n') {
      ++line_number_;
    }
    ++pos_;
  }
  return next;
}

Parser::StateFunc Parser::SyntaxError(const std::string& message) {
  delegate_->OnSyntaxError(message);
  return NULL;
}

void Parser::DataError(const std::string& error) {
  data_error_ = true;
  delegate_->OnDataError(error);
}

}  // namespace test_expectations