1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef BASE_TEST_EXPECTATIONS_PARSER_H_
6 #define BASE_TEST_EXPECTATIONS_PARSER_H_
10 #include "base/basictypes.h"
11 #include "base/strings/string_piece.h"
12 #include "base/test/expectations/expectation.h"
14 namespace test_expectations
{
16 // This is the internal parser for test expectations. It parses an input
17 // string and reports information to its Delegate as it's processing the
20 // The input format is documented here:
21 // https://docs.google.com/a/chromium.org/document/d/1edhMJ5doY_dzfbKNCzeJJ-8XxPrexTbNL2Y_jVvLB8Q/view
24 // "http://bug/1234 [ OS-Version ] Test.Name = Result"
26 // The parser is implemented as a state machine, with each state returning a
27 // function pointer to the next state.
30 // The parser will call these methods on its delegate during a Parse()
34 // When a well-formed and valid Expectation has been parsed from the input,
35 // it is reported to the delegate via this method.
36 virtual void EmitExpectation(const Expectation
& expectation
) = 0;
38 // Called when the input string is not well-formed. Parsing will stop after
39 // this method is called.
40 virtual void OnSyntaxError(const std::string
& message
) = 0;
42 // Called when an Expectation has been parsed because it is well-formed but
43 // contains invalid data (i.e. the modifiers or result are not valid
44 // keywords). This Expectation will not be reported via EmitExpectation.
45 virtual void OnDataError(const std::string
& message
) = 0;
48 // Creates a new parser for |input| that will send data to |delegate|.
49 Parser(Delegate
* delegate
, const std::string
& input
);
52 // Runs the parser of the input string.
56 // This bit of hackery is used to implement a function pointer type that
57 // returns a pointer to a function of the same signature. Since a definition
58 // like that is inherently recursive, it's impossible to do:
59 // type StateFunc(*StateFunc)(StateData*);
60 // However, this approach works without the need to use void*. Inspired by
61 // <http://www.gotw.ca/gotw/057.htm>.
63 typedef StateFunc(Parser::*StateFuncPtr
)();
65 StateFunc(StateFuncPtr pf
) : pf_(pf
) {}
66 operator StateFuncPtr() {
72 // Tests whether there is at least one more character at pos_ before end_.
75 // The parser state functions. On entry, the parser state is at the beginning
76 // of the token. Each returns a function pointer to the next state function,
77 // or NULL to end parsing. On return, the parser is at the beginning of the
80 StateFunc
ParseComment();
81 StateFunc
ParseBugURL();
82 StateFunc
BeginModifiers();
83 StateFunc
InModifiers();
84 StateFunc
SaveModifier();
85 StateFunc
EndModifiers();
86 StateFunc
ParseTestName();
87 StateFunc
SaveTestName();
88 StateFunc
ParseExpectation();
89 StateFunc
ParseExpectationType();
90 StateFunc
SaveExpectationType();
93 // A state function that collects character data from the current position
94 // to the next whitespace character. Returns the |success| function when at
95 // the end of the string, with the data stored in |extracted_string_|.
96 StateFunc
ExtractString(StateFunc success
);
98 // Function that skips over horizontal whitespace characters and then returns
100 StateFunc
SkipWhitespace(StateFunc next
);
102 // Does the same as SkipWhitespace but includes newlines.
103 StateFunc
SkipWhitespaceAndNewLines(StateFunc next
);
105 // State function that reports the given syntax error |message| to the
106 // delegate and then returns NULL, ending the parse loop.
107 StateFunc
SyntaxError(const std::string
& message
);
109 // Function that reports the data |error| to the delegate without stopping
111 void DataError(const std::string
& error
);
119 // Current location in the |input_|.
122 // Pointer to the end of the |input_|.
125 // Current line number, as updated by SkipWhitespace().
128 // The character data extracted from |input_| as a result of the
129 // ExtractString() state.
130 base::StringPiece extracted_string_
;
132 // The Expectation object that is currently being processed by the parser.
134 Expectation current_
;
136 // If DataError() has been called during the course of parsing |current_|.
137 // If true, then |current_| will not be emitted to the Delegate.
141 } // namespace test_expectations
143 #endif // BASE_TEST_EXPECTATIONS_PARSER_H_