mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-23 23:42:42 +00:00
When originally switching to the table dispatch approach we discussed that it'd be nice to disentangle the monolithic symbol lexing routine with this as we'll typically have fairly precise dispatch. This is especially true for grouping symbols, which in Carbon are all constructively one-character (at this point). I think this provides a substantial improvement to the clarity of the code by disentangling the different paths. It also allowed a bunch of simplifications / clarifications to exactly what the behavior with closing invalid groups actually involves currently. This was initially motivated by code organization improvements, and any performance wins were speculative. However, when benchmarking it surfaced a problem that hadn't been clear -- we're generating too many distinct functions here, and the table-based dispatch slows down in the face of that. So this PR also includes a fix for that, removing the template-generated fan-out of dispatch functions for distinct symbols. Instead, we have a dedicated table to translate one character into the token kinds. This seems to work quite well, avoiding the huge branch-y structure and just do fairly cheap table translation & dispatch for all one-character symbols. Building the table requires the token kinds to be default constructable, so this also enables that and arranges for the zero-value kind to be the error kind. Combined, this is a modest speedup for *non* grouping symbols (3-4%, a bit noisy). And in some cases it is a huge speedup for grouping symbols (>10%). Raw benchmark data with 20 runs before/after -- despite the # of runs, the grouping symbols benchmarks were frustratingly noisy in non-uniform ways that couldn't fully be accounted for here. Still, this seems like an overall improvement. ``` BM_RandomSource 7.98ms ± 2% 7.73ms ± 3% -3.12% (p=0.000 n=18+19) BM_GroupingSymbols/1/0/0 5.90ms ± 2% 5.82ms ± 4% -1.38% (p=0.001 n=20+20) BM_GroupingSymbols/2/0/0 5.21ms ± 2% 5.15ms ± 2% -1.14% (p=0.002 n=20+18) BM_GroupingSymbols/3/0/0 4.42ms ± 2% 4.34ms ± 2% -1.87% (p=0.000 n=19+18) BM_GroupingSymbols/4/0/0 4.29ms ± 2% 4.38ms ± 5% ~ (p=0.297 n=17+20) BM_GroupingSymbols/8/0/0 5.09ms ±10% 5.10ms ± 7% ~ (p=0.919 n=18+20) BM_GroupingSymbols/16/0/0 6.35ms ± 8% 6.29ms ± 6% ~ (p=0.201 n=20+20) BM_GroupingSymbols/32/0/0 9.88ms ± 2% 9.83ms ± 1% ~ (p=0.167 n=18+20) BM_GroupingSymbols/0/1/0 5.12ms ± 2% 5.01ms ± 2% -2.14% (p=0.000 n=20+19) BM_GroupingSymbols/0/2/0 4.01ms ± 2% 3.93ms ± 4% -2.03% (p=0.000 n=20+19) BM_GroupingSymbols/0/3/0 2.92ms ± 3% 2.81ms ± 2% -3.87% (p=0.000 n=20+19) BM_GroupingSymbols/0/4/0 2.61ms ± 3% 2.47ms ± 2% -5.30% (p=0.000 n=20+18) BM_GroupingSymbols/0/8/0 1.77ms ± 3% 1.61ms ± 2% -8.91% (p=0.000 n=18+19) BM_GroupingSymbols/0/16/0 1.41ms ± 3% 1.16ms ± 4% -17.66% (p=0.000 n=20+20) BM_GroupingSymbols/0/32/0 1.10ms ± 2% 0.92ms ± 3% -16.36% (p=0.000 n=20+17) BM_GroupingSymbols/0/0/1 5.09ms ± 2% 5.03ms ± 3% -1.11% (p=0.001 n=20+18) BM_GroupingSymbols/0/0/2 4.01ms ± 2% 3.91ms ± 2% -2.67% (p=0.000 n=20+18) BM_GroupingSymbols/0/0/3 2.93ms ± 3% 2.81ms ± 2% -4.23% (p=0.000 n=20+19) BM_GroupingSymbols/0/0/4 2.59ms ± 2% 2.48ms ± 3% -4.48% (p=0.000 n=20+19) BM_GroupingSymbols/0/0/8 1.75ms ± 1% 1.62ms ± 3% -7.65% (p=0.000 n=17+19) BM_GroupingSymbols/0/0/16 1.40ms ± 2% 1.15ms ± 3% -17.67% (p=0.000 n=19+20) BM_GroupingSymbols/0/0/32 1.10ms ± 2% 0.92ms ± 3% -15.91% (p=0.000 n=20+19) BM_GroupingSymbols/32/1/0 9.62ms ± 2% 9.65ms ± 2% ~ (p=0.654 n=18+20) BM_GroupingSymbols/32/2/0 9.41ms ± 2% 9.37ms ± 2% ~ (p=0.095 n=20+19) BM_GroupingSymbols/32/3/0 9.13ms ± 2% 9.13ms ± 3% ~ (p=0.687 n=19+20) BM_GroupingSymbols/32/4/0 8.93ms ± 1% 8.87ms ± 2% -0.69% (p=0.010 n=20+18) BM_GroupingSymbols/32/8/0 8.15ms ± 2% 8.14ms ± 3% ~ (p=0.729 n=19+19) BM_GroupingSymbols/32/16/0 7.04ms ± 3% 6.92ms ± 1% -1.71% (p=0.000 n=20+18) BM_GroupingSymbols/32/32/0 5.48ms ± 2% 5.38ms ± 3% -1.81% (p=0.000 n=20+20) BM_GroupingSymbols/32/32/1 5.39ms ± 2% 5.29ms ± 2% -1.87% (p=0.000 n=19+19) BM_GroupingSymbols/32/32/2 5.34ms ± 2% 5.21ms ± 1% -2.45% (p=0.000 n=20+18) BM_GroupingSymbols/32/32/3 5.27ms ± 3% 5.16ms ± 2% -2.18% (p=0.000 n=20+19) BM_GroupingSymbols/32/32/4 5.21ms ± 2% 5.10ms ± 3% -2.11% (p=0.000 n=19+20) BM_GroupingSymbols/32/32/8 4.98ms ± 2% 4.83ms ± 2% -2.85% (p=0.000 n=19+19) BM_GroupingSymbols/32/32/16 4.55ms ± 2% 4.45ms ± 2% -2.25% (p=0.000 n=18+20) BM_GroupingSymbols/32/32/32 3.95ms ± 2% 3.84ms ± 2% -2.98% (p=0.000 n=19+20) ```
221 lines
9.4 KiB
Modula-2
221 lines
9.4 KiB
Modula-2
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
// This is an X-macro header. It does not use `#include` guards, and instead is
|
|
// designed to be `#include`ed after the x-macro is defined in order for its
|
|
// inclusion to expand to the desired output. Macro definitions are cleaned up
|
|
// at the end of this file.
|
|
//
|
|
// Supported x-macros are:
|
|
// - CARBON_TOKEN(Name)
|
|
// Defines a token. Used directly when a token needs custom parsing, such as
|
|
// integer literals.
|
|
// - CARBON_SYMBOL_TOKEN(Name, Spelling)
|
|
// Defines a symbol which has the provided spelling, such as `*`. Spellings
|
|
// must be unique.
|
|
// - CARBON_OPENING_GROUP_SYMBOL_TOKEN(Name, Spelling, ClosingName)
|
|
// - CARBON_CLOSING_GROUP_SYMBOL_TOKEN(Name, Spelling, OpeningName)
|
|
// These two macros together define matches opening and closing symbols,
|
|
// such as `(` and `)`, and create an association between the two.
|
|
// - CARBON_KEYWORD_TOKEN(Name, Spelling)
|
|
// Defines a keyword which has the provided spelling, such as `if`.
|
|
// Spellings must be unique.
|
|
// - CARBON_TOKEN_WITH_VIRTUAL_NODE(Token)
|
|
// Wrapped around one of the abokve _TOKEN macros, indicates that this
|
|
// token has one additional virtual node in the parse tree.
|
|
//
|
|
// This tree represents the subset relationship between these macros, where if a
|
|
// specific x-macro isn't defined, it'll fall back to the parent macro.
|
|
|
|
#ifndef CARBON_TOKEN
|
|
#define CARBON_TOKEN(Name)
|
|
#endif
|
|
|
|
// The error token comes first because we want it to get the zero value, which
|
|
// will also be used in default initialization.
|
|
CARBON_TOKEN(Error)
|
|
|
|
#ifndef CARBON_SYMBOL_TOKEN
|
|
#define CARBON_SYMBOL_TOKEN(Name, Spelling) CARBON_TOKEN(Name)
|
|
#endif
|
|
|
|
#ifndef CARBON_ONE_CHAR_SYMBOL_TOKEN
|
|
#define CARBON_ONE_CHAR_SYMBOL_TOKEN(Name, Spelling) \
|
|
CARBON_SYMBOL_TOKEN(Name, Spelling)
|
|
#endif
|
|
|
|
#ifndef CARBON_TOKEN_WITH_VIRTUAL_NODE
|
|
#define CARBON_TOKEN_WITH_VIRTUAL_NODE(Name) Name
|
|
#endif
|
|
|
|
// Note that symbols need to be ordered from longest to shortest to effectively
|
|
// provide max-munch lexing.
|
|
|
|
// clang-format off
|
|
|
|
CARBON_SYMBOL_TOKEN(GreaterGreaterEqual, ">>=")
|
|
CARBON_SYMBOL_TOKEN(LessEqualGreater, "<=>")
|
|
CARBON_SYMBOL_TOKEN(LessLessEqual, "<<=")
|
|
|
|
CARBON_SYMBOL_TOKEN(AmpEqual, "&=")
|
|
CARBON_SYMBOL_TOKEN(CaretEqual, "^=")
|
|
CARBON_SYMBOL_TOKEN(ColonEqual, ":=")
|
|
CARBON_SYMBOL_TOKEN(ColonExclaim, ":!")
|
|
CARBON_SYMBOL_TOKEN(EqualEqual, "==")
|
|
CARBON_SYMBOL_TOKEN(EqualGreater, "=>")
|
|
CARBON_SYMBOL_TOKEN(ExclaimEqual, "!=")
|
|
CARBON_SYMBOL_TOKEN(GreaterEqual, ">=")
|
|
CARBON_SYMBOL_TOKEN(GreaterGreater, ">>")
|
|
CARBON_SYMBOL_TOKEN(LessEqual, "<=")
|
|
CARBON_SYMBOL_TOKEN(LessGreater, "<>")
|
|
CARBON_SYMBOL_TOKEN(LessLess, "<<")
|
|
CARBON_SYMBOL_TOKEN(LessMinus, "<-")
|
|
CARBON_SYMBOL_TOKEN(MinusEqual, "-=")
|
|
CARBON_SYMBOL_TOKEN(MinusGreater, "->")
|
|
CARBON_SYMBOL_TOKEN(MinusMinus, "--")
|
|
CARBON_SYMBOL_TOKEN(PercentEqual, "%=")
|
|
CARBON_SYMBOL_TOKEN(PipeEqual, "|=")
|
|
CARBON_SYMBOL_TOKEN(PlusEqual, "+=")
|
|
CARBON_SYMBOL_TOKEN(PlusPlus, "++")
|
|
CARBON_SYMBOL_TOKEN(SlashEqual, "/=")
|
|
CARBON_SYMBOL_TOKEN(StarEqual, "*=")
|
|
CARBON_SYMBOL_TOKEN(TildeEqual, "~=")
|
|
|
|
CARBON_SYMBOL_TOKEN(Amp, "&")
|
|
CARBON_SYMBOL_TOKEN(At, "@")
|
|
CARBON_SYMBOL_TOKEN(Backslash, "\\")
|
|
CARBON_SYMBOL_TOKEN(Caret, "^")
|
|
CARBON_SYMBOL_TOKEN(Colon, ":")
|
|
CARBON_SYMBOL_TOKEN(Equal, "=")
|
|
CARBON_SYMBOL_TOKEN(Exclaim, "!")
|
|
CARBON_SYMBOL_TOKEN(Greater, ">")
|
|
CARBON_SYMBOL_TOKEN(Less, "<")
|
|
CARBON_SYMBOL_TOKEN(Minus, "-")
|
|
CARBON_SYMBOL_TOKEN(Percent, "%")
|
|
CARBON_SYMBOL_TOKEN(Period, ".")
|
|
CARBON_SYMBOL_TOKEN(Pipe, "|")
|
|
CARBON_SYMBOL_TOKEN(Plus, "+")
|
|
CARBON_SYMBOL_TOKEN(Question, "?")
|
|
CARBON_SYMBOL_TOKEN(Slash, "/")
|
|
CARBON_SYMBOL_TOKEN(Star, "*")
|
|
CARBON_SYMBOL_TOKEN(Tilde, "~")
|
|
|
|
// Some Carbon symbols are constructively exactly one character and cannot be
|
|
// combined with any other characters to form new symbols. We can lex these
|
|
// without needing to max-munch any other characters. These are typically
|
|
// expected to be terminators or separators that need to compose with all other
|
|
// parts of the grammar. Group symbols are also currently one-character symbols,
|
|
// although we may choose to remove that if we need to add composite grouping
|
|
// symbols in the future.
|
|
CARBON_ONE_CHAR_SYMBOL_TOKEN(Comma, ",")
|
|
CARBON_ONE_CHAR_SYMBOL_TOKEN(Semi, ";")
|
|
|
|
// clang-format on
|
|
|
|
#ifndef CARBON_OPENING_GROUP_SYMBOL_TOKEN
|
|
#define CARBON_OPENING_GROUP_SYMBOL_TOKEN(Name, Spelling, ClosingName) \
|
|
CARBON_ONE_CHAR_SYMBOL_TOKEN(Name, Spelling)
|
|
#endif
|
|
CARBON_OPENING_GROUP_SYMBOL_TOKEN(OpenParen, "(", CloseParen)
|
|
CARBON_OPENING_GROUP_SYMBOL_TOKEN(OpenCurlyBrace, "{", CloseCurlyBrace)
|
|
CARBON_OPENING_GROUP_SYMBOL_TOKEN(OpenSquareBracket, "[", CloseSquareBracket)
|
|
#undef CARBON_OPENING_GROUP_SYMBOL_TOKEN
|
|
|
|
#ifndef CARBON_CLOSING_GROUP_SYMBOL_TOKEN
|
|
#define CARBON_CLOSING_GROUP_SYMBOL_TOKEN(Name, Spelling, OpeningName) \
|
|
CARBON_ONE_CHAR_SYMBOL_TOKEN(Name, Spelling)
|
|
#endif
|
|
CARBON_CLOSING_GROUP_SYMBOL_TOKEN(CloseParen, ")", OpenParen)
|
|
CARBON_CLOSING_GROUP_SYMBOL_TOKEN(CloseCurlyBrace, "}", OpenCurlyBrace)
|
|
CARBON_CLOSING_GROUP_SYMBOL_TOKEN(CloseSquareBracket, "]", OpenSquareBracket)
|
|
#undef CARBON_CLOSING_GROUP_SYMBOL_TOKEN
|
|
|
|
#undef CARBON_ONE_CHAR_SYMBOL_TOKEN
|
|
#undef CARBON_SYMBOL_TOKEN
|
|
|
|
#ifndef CARBON_KEYWORD_TOKEN
|
|
#define CARBON_KEYWORD_TOKEN(Name, Spelling) CARBON_TOKEN(Name)
|
|
#endif
|
|
// clang-format off
|
|
CARBON_KEYWORD_TOKEN(Abstract, "abstract")
|
|
CARBON_KEYWORD_TOKEN(Adapt, "adapt")
|
|
CARBON_KEYWORD_TOKEN(Addr, "addr")
|
|
CARBON_KEYWORD_TOKEN(Alias, "alias")
|
|
CARBON_TOKEN_WITH_VIRTUAL_NODE(
|
|
CARBON_KEYWORD_TOKEN(And, "and"))
|
|
CARBON_KEYWORD_TOKEN(Api, "api")
|
|
CARBON_KEYWORD_TOKEN(As, "as")
|
|
CARBON_KEYWORD_TOKEN(Auto, "auto")
|
|
CARBON_KEYWORD_TOKEN(Base, "base")
|
|
CARBON_KEYWORD_TOKEN(Bool, "bool")
|
|
CARBON_KEYWORD_TOKEN(Break, "break")
|
|
CARBON_KEYWORD_TOKEN(Case, "case")
|
|
CARBON_KEYWORD_TOKEN(Choice, "choice")
|
|
CARBON_KEYWORD_TOKEN(Class, "class")
|
|
CARBON_KEYWORD_TOKEN(Const, "const")
|
|
CARBON_KEYWORD_TOKEN(Constraint, "constraint")
|
|
CARBON_KEYWORD_TOKEN(Continue, "continue")
|
|
CARBON_KEYWORD_TOKEN(Default, "default")
|
|
CARBON_KEYWORD_TOKEN(Destructor, "destructor")
|
|
CARBON_KEYWORD_TOKEN(Else, "else")
|
|
CARBON_KEYWORD_TOKEN(Extend, "extend")
|
|
CARBON_KEYWORD_TOKEN(False, "false")
|
|
CARBON_KEYWORD_TOKEN(Final, "final")
|
|
CARBON_KEYWORD_TOKEN(Fn, "fn")
|
|
CARBON_KEYWORD_TOKEN(For, "for")
|
|
CARBON_KEYWORD_TOKEN(Forall, "forall")
|
|
CARBON_KEYWORD_TOKEN(Friend, "friend")
|
|
CARBON_KEYWORD_TOKEN(If, "if")
|
|
CARBON_KEYWORD_TOKEN(Impl, "impl")
|
|
CARBON_KEYWORD_TOKEN(Impls, "impls")
|
|
CARBON_KEYWORD_TOKEN(Import, "import")
|
|
CARBON_KEYWORD_TOKEN(In, "in")
|
|
CARBON_KEYWORD_TOKEN(Interface, "interface")
|
|
CARBON_KEYWORD_TOKEN(Let, "let")
|
|
CARBON_KEYWORD_TOKEN(Library, "library")
|
|
CARBON_KEYWORD_TOKEN(Like, "like")
|
|
CARBON_KEYWORD_TOKEN(Match, "match")
|
|
CARBON_KEYWORD_TOKEN(Namespace, "namespace")
|
|
CARBON_KEYWORD_TOKEN(Not, "not")
|
|
CARBON_KEYWORD_TOKEN(Observe, "observe")
|
|
CARBON_TOKEN_WITH_VIRTUAL_NODE(
|
|
CARBON_KEYWORD_TOKEN(Or, "or"))
|
|
CARBON_KEYWORD_TOKEN(Override, "override")
|
|
CARBON_KEYWORD_TOKEN(Package, "package")
|
|
CARBON_KEYWORD_TOKEN(Partial, "partial")
|
|
CARBON_KEYWORD_TOKEN(Private, "private")
|
|
CARBON_KEYWORD_TOKEN(Protected, "protected")
|
|
CARBON_KEYWORD_TOKEN(Require, "require")
|
|
CARBON_KEYWORD_TOKEN(Return, "return")
|
|
CARBON_KEYWORD_TOKEN(Returned, "returned")
|
|
CARBON_KEYWORD_TOKEN(SelfTypeIdentifier, "Self")
|
|
CARBON_KEYWORD_TOKEN(SelfValueIdentifier, "self")
|
|
// TODO: Although we provide a String type literal, it's not standardized.
|
|
CARBON_KEYWORD_TOKEN(StringTypeLiteral, "String")
|
|
CARBON_KEYWORD_TOKEN(Template, "template")
|
|
CARBON_KEYWORD_TOKEN(Then, "then")
|
|
CARBON_KEYWORD_TOKEN(True, "true")
|
|
CARBON_KEYWORD_TOKEN(Type, "type")
|
|
// Underscore is tokenized as a keyword because it's part of identifiers.
|
|
CARBON_KEYWORD_TOKEN(Underscore, "_")
|
|
CARBON_KEYWORD_TOKEN(Var, "var")
|
|
CARBON_KEYWORD_TOKEN(Virtual, "virtual")
|
|
CARBON_KEYWORD_TOKEN(Where, "where")
|
|
CARBON_KEYWORD_TOKEN(While, "while")
|
|
// clang-format on
|
|
#undef CARBON_KEYWORD_TOKEN
|
|
|
|
CARBON_TOKEN(Identifier)
|
|
CARBON_TOKEN(IntegerLiteral)
|
|
CARBON_TOKEN(RealLiteral)
|
|
CARBON_TOKEN(StringLiteral)
|
|
CARBON_TOKEN(IntegerTypeLiteral)
|
|
CARBON_TOKEN(UnsignedIntegerTypeLiteral)
|
|
CARBON_TOKEN(FloatingPointTypeLiteral)
|
|
CARBON_TOKEN(StartOfFile)
|
|
CARBON_TOKEN(EndOfFile)
|
|
|
|
#undef CARBON_TOKEN
|
|
#undef CARBON_TOKEN_WITH_VIRTUAL_NODE
|