Skip to content

Instantly share code, notes, and snippets.

Created March 14, 2015 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4f19ab8e8127394cce09 to your computer and use it in GitHub Desktop.
Save anonymous/4f19ab8e8127394cce09 to your computer and use it in GitHub Desktop.
template <typename T>
struct Lexer : boost::spirit::lex::lexer<T>
{
Lexer(char const* start) : row(1), rowStart(start)
{
using boost::spirit::lex::token_def;
using boost::phoenix::ref;
using boost::spirit::lex::_end;
this->self
// Whitespace
// How do you save current line iterator for column?
= token_def<>("[\n]", Whitespace)[++ref(row), ref(rowStart) = _end]
| token_def<>("[ \r\t]", Whitespace)
// Keywords must appear before identifier, or else they get tagged as identifiers
| token_def<>("var", Var)
| token_def<>("if", If)
| token_def<>("else", Else)
| token_def<>("for", For)
| token_def<>("while", While)
| token_def<>("return", Return)
// Types like keywords must appear before identifier
| token_def<>("bool", Bool)
| token_def<>("int8", Int8)
| token_def<>("int16", Int16)
| token_def<>("int32", Int32)
| token_def<>("int64", Int64)
| token_def<>("string", String)
| token_def<>("uint8", UInt8)
| token_def<>("uint16", UInt16)
| token_def<>("uint32", UInt32)
| token_def<>("uint64", UInt64)
| token_def<>("void", Void)
| token_def<>("[a-zA-Z][a-zA-Z0-9]*", Identifier)
| token_def<>("0b[0-1]+", BinLiteral)
| token_def<>("0o[0-7]+", OctLiteral)
| token_def<>("0x[0-9a-fA-F]+", HexLiteral)
| token_def<>("(0d)?[0-9]+", DecLiteral)
| token_def<>("\\\".*\\\"", StringLiteral)
| token_def<>("\\+", Add)
| token_def<>("\\-", Subtract)
| token_def<>("\\*", Multiply)
| token_def<>("\\/", Divide)
| token_def<>("<=", LowerEqual)
| token_def<>("<", Lower)
| token_def<>(">=", GreaterEqual)
| token_def<>(">", Greater)
| token_def<>("==", Equal)
| token_def<>("!=", NotEqual)
| token_def<>("=", Assign)
| token_def<>("\\(", ParenthesisOpen)
| token_def<>("\\)", ParenthesisClose)
| token_def<>("\\{", CurlyBraceOpen)
| token_def<>("\\}", CurlyBraceClose)
| token_def<>(";", Semicolon)
| token_def<>(",", Comma);
}
size_t row;
char const* rowStart;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment