//===- Lexer.cpp - MLIR Lexer Implementation ------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This file implements the lexer for the MLIR textual form. // //===----------------------------------------------------------------------===// #include "Lexer.h" #include "Token.h" #include "mlir/AsmParser/CodeComplete.h" #include "mlir/IR/Diagnostics.h" #include "mlir/IR/Location.h" #include "mlir/IR/MLIRContext.h" #include "mlir/Support/LLVM.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringSwitch.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/SourceMgr.h" #include <cassert> #include <cctype> usingnamespacemlir; // Returns true if 'c' is an allowable punctuation character: [$._-] // Returns false otherwise. static bool isPunct(char c) { … } Lexer::Lexer(const llvm::SourceMgr &sourceMgr, MLIRContext *context, AsmParserCodeCompleteContext *codeCompleteContext) : … { … } /// Encode the specified source location information into an attribute for /// attachment to the IR. Location Lexer::getEncodedSourceLocation(SMLoc loc) { … } /// emitError - Emit an error message and return an Token::error token. Token Lexer::emitError(const char *loc, const Twine &message) { … } Token Lexer::lexToken() { … } /// Lex an '@foo' identifier. /// /// symbol-ref-id ::= `@` (bare-id | string-literal) /// Token Lexer::lexAtIdentifier(const char *tokStart) { … } /// Lex a bare identifier or keyword that starts with a letter. /// /// bare-id ::= (letter|[_]) (letter|digit|[_$.])* /// integer-type ::= `[su]?i[1-9][0-9]*` /// Token Lexer::lexBareIdentifierOrKeyword(const char *tokStart) { … } /// Skip a comment line, starting with a '//'. /// /// TODO: add a regex for comments here and to the spec. /// void Lexer::skipComment() { … } /// Lex an ellipsis. /// /// ellipsis ::= '...' /// Token Lexer::lexEllipsis(const char *tokStart) { … } /// Lex a number literal. /// /// integer-literal ::= digit+ | `0x` hex_digit+ /// float-literal ::= [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)? /// Token Lexer::lexNumber(const char *tokStart) { … } /// Lex an identifier that starts with a prefix followed by suffix-id. /// /// attribute-id ::= `#` suffix-id /// ssa-id ::= '%' suffix-id /// block-id ::= '^' suffix-id /// type-id ::= '!' suffix-id /// suffix-id ::= digit+ | (letter|id-punct) (letter|id-punct|digit)* /// id-punct ::= `$` | `.` | `_` | `-` /// Token Lexer::lexPrefixedIdentifier(const char *tokStart) { … } /// Lex a string literal. /// /// string-literal ::= '"' [^"\n\f\v\r]* '"' /// /// TODO: define escaping rules. Token Lexer::lexString(const char *tokStart) { … }