[mlir] Add plus, star and optional less/greater parsing

The tokens are already handled by the lexer. This revision exposes them
through the parser interface.

This revision also adds missing functions for question mark parsing and
completes the list of valid punctuation tokens in the documentation.

Differential Revision: https://reviews.llvm.org/D90907
This commit is contained in:
Jean-Michel Gorius 2020-11-11 18:01:39 +01:00
parent b9d36540a8
commit e47805c995
7 changed files with 73 additions and 15 deletions

View File

@ -768,7 +768,7 @@ A literal is either a keyword or punctuation surrounded by \`\`.
The following are the set of valid punctuation:
`:`, `,`, `=`, `<`, `>`, `(`, `)`, `{`, `}`, `[`, `]`, `->`
`:`, `,`, `=`, `<`, `>`, `(`, `)`, `{`, `}`, `[`, `]`, `->`, `?`, `+`, `*`
#### Variables

View File

@ -326,9 +326,33 @@ public:
/// Parse a '<' token.
virtual ParseResult parseLess() = 0;
/// Parse a '<' token if present.
virtual ParseResult parseOptionalLess() = 0;
/// Parse a '>' token.
virtual ParseResult parseGreater() = 0;
/// Parse a '>' token if present.
virtual ParseResult parseOptionalGreater() = 0;
/// Parse a '?' token.
virtual ParseResult parseQuestion() = 0;
/// Parse a '?' token if present.
virtual ParseResult parseOptionalQuestion() = 0;
/// Parse a '+' token.
virtual ParseResult parsePlus() = 0;
/// Parse a '+' token if present.
virtual ParseResult parseOptionalPlus() = 0;
/// Parse a '*' token.
virtual ParseResult parseStar() = 0;
/// Parse a '*' token if present.
virtual ParseResult parseOptionalStar() = 0;
/// Parse a given keyword.
ParseResult parseKeyword(StringRef keyword, const Twine &msg = "") {
auto loc = getCurrentLocation();
@ -369,9 +393,6 @@ public:
/// Parse a `)` token if present.
virtual ParseResult parseOptionalRParen() = 0;
/// Parses a '?' if present.
virtual ParseResult parseOptionalQuestion() = 0;
/// Parse a `[` token.
virtual ParseResult parseLSquare() = 0;

View File

@ -994,11 +994,21 @@ public:
return parser.parseToken(Token::less, "expected '<'");
}
/// Parse a '<' token if present.
ParseResult parseOptionalLess() override {
return success(parser.consumeIf(Token::less));
}
/// Parse a '>' token.
ParseResult parseGreater() override {
return parser.parseToken(Token::greater, "expected '>'");
}
/// Parse a '>' token if present.
ParseResult parseOptionalGreater() override {
return success(parser.consumeIf(Token::greater));
}
/// Parse a `(` token.
ParseResult parseLParen() override {
return parser.parseToken(Token::l_paren, "expected '('");
@ -1019,11 +1029,6 @@ public:
return success(parser.consumeIf(Token::r_paren));
}
/// Parses a '?' if present.
ParseResult parseOptionalQuestion() override {
return success(parser.consumeIf(Token::question));
}
/// Parse a `[` token.
ParseResult parseLSquare() override {
return parser.parseToken(Token::l_square, "expected '['");
@ -1044,6 +1049,36 @@ public:
return success(parser.consumeIf(Token::r_square));
}
/// Parses a '?' token.
ParseResult parseQuestion() override {
return parser.parseToken(Token::question, "expected '?'");
}
/// Parses a '?' token if present.
ParseResult parseOptionalQuestion() override {
return success(parser.consumeIf(Token::question));
}
/// Parses a '+' token.
ParseResult parsePlus() override {
return parser.parseToken(Token::plus, "expected '+'");
}
/// Parses a '+' token if present.
ParseResult parseOptionalPlus() override {
return success(parser.consumeIf(Token::plus));
}
/// Parses a '*' token.
ParseResult parseStar() override {
return parser.parseToken(Token::star, "expected '*'");
}
/// Parses a '*' token if present.
ParseResult parseOptionalStar() override {
return success(parser.consumeIf(Token::star));
}
//===--------------------------------------------------------------------===//
// Attribute Parsing
//===--------------------------------------------------------------------===//

View File

@ -1380,7 +1380,7 @@ def AsmDialectInterfaceOp : TEST_Op<"asm_dialect_interface_op"> {
def FormatLiteralOp : TEST_Op<"format_literal_op"> {
let assemblyFormat = [{
`keyword_$.` `->` `:` `,` `=` `<` `>` `(` `)` `[` `]` `` `(` ` ` `)` attr-dict
`keyword_$.` `->` `:` `,` `=` `<` `>` `(` `)` `[` `]` `` `(` ` ` `)` `?` `+` `*` attr-dict
}];
}

View File

@ -309,7 +309,7 @@ def LiteralInvalidB : TestFormat_Op<"literal_invalid_b", [{
}]>;
// CHECK-NOT: error
def LiteralValid : TestFormat_Op<"literal_valid", [{
`_` `:` `,` `=` `<` `>` `(` `)` `[` `]` ` ` `` `->` `abc$._`
`_` `:` `,` `=` `<` `>` `(` `)` `[` `]` `?` `+` `*` ` ` `` `->` `abc$._`
attr-dict
}]>;

View File

@ -5,8 +5,8 @@
// CHECK: %[[MEMREF:.*]] =
%memref = "foo.op"() : () -> (memref<1xf64>)
// CHECK: test.format_literal_op keyword_$. -> :, = <> () []( ) {foo.some_attr}
test.format_literal_op keyword_$. -> :, = <> () []( ) {foo.some_attr}
// CHECK: test.format_literal_op keyword_$. -> :, = <> () []( ) ? + * {foo.some_attr}
test.format_literal_op keyword_$. -> :, = <> () []( ) ? + * {foo.some_attr}
// CHECK: test.format_attr_op 10
// CHECK-NOT: {attr

View File

@ -281,7 +281,7 @@ bool LiteralElement::isValidLiteral(StringRef value) {
// If there is only one character, this must either be punctuation or a
// single character bare identifier.
if (value.size() == 1)
return isalpha(front) || StringRef("_:,=<>()[]{}?").contains(front);
return isalpha(front) || StringRef("_:,=<>()[]{}?+*").contains(front);
// Check the punctuation that are larger than a single character.
if (value == "->")
@ -762,7 +762,9 @@ static void genLiteralParser(StringRef value, OpMethodBody &body) {
.Case(")", "RParen()")
.Case("[", "LSquare()")
.Case("]", "RSquare()")
.Case("?", "Question()");
.Case("?", "Question()")
.Case("+", "Plus()")
.Case("*", "Star()");
}
/// Generate the storage code required for parsing the given element.