Skip to content

Commit 0af1149

Browse files
jgravelle-googlebinji
authored andcommitted
Initial custom annotation support (#1076)
Lex custom annotations, but discard them in the parser. In the future we should be able to parse some of them, but this is simple and spec-compliant for now.
1 parent f461492 commit 0af1149

13 files changed

+74
-2
lines changed

src/feature.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@ WABT_FEATURE(multi_value, "multi-value", false, "Multi-value"
3232
WABT_FEATURE(tail_call, "tail-call", false, "Tail-call support")
3333
WABT_FEATURE(bulk_memory, "bulk-memory", false, "Bulk-memory operations")
3434
WABT_FEATURE(reference_types, "reference-types", false, "Reference types (anyref)")
35+
WABT_FEATURE(annotations, "annotations", false, "Custom annotation syntax")

src/token.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ WABT_TOKEN_LAST(Opcode, Unreachable)
137137

138138
/* Tokens with string data. */
139139
WABT_TOKEN(AlignEqNat, "align=")
140+
WABT_TOKEN(LparAnn, "Annotation")
140141
WABT_TOKEN(OffsetEqNat, "offset=")
141142
WABT_TOKEN(Reserved, "Reserved")
142143
WABT_TOKEN(Text, "TEXT")

src/wast-lexer.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ Token WastLexer::GetToken(WastParser* parser) {
6464
continue;
6565
}
6666
return BareToken(TokenType::Eof);
67+
} else if (MatchString("(@")) {
68+
ReadReservedChars();
69+
// offset=2 to skip the "(@" prefix
70+
return TextToken(TokenType::LparAnn, 2);
6771
} else {
6872
ReadChar();
6973
return BareToken(TokenType::Lpar);

src/wast-parser.cc

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,8 +391,35 @@ Location WastParser::GetLocation() {
391391
}
392392

393393
TokenType WastParser::Peek(size_t n) {
394-
while (tokens_.size() <= n)
395-
tokens_.push_back(lexer_->GetToken(this));
394+
while (tokens_.size() <= n) {
395+
Token cur = lexer_->GetToken(this);
396+
if (cur.token_type() != TokenType::LparAnn) {
397+
tokens_.push_back(cur);
398+
} else {
399+
// Custom annotation. For now, discard until matching Rpar
400+
if (!options_->features.annotations_enabled()) {
401+
Error(cur.loc, "annotations not enabled: %s", cur.to_string().c_str());
402+
return TokenType::Invalid;
403+
}
404+
int indent = 1;
405+
while (indent > 0) {
406+
cur = lexer_->GetToken(this);
407+
switch (cur.token_type()) {
408+
case TokenType::Lpar:
409+
case TokenType::LparAnn:
410+
indent++;
411+
break;
412+
413+
case TokenType::Rpar:
414+
indent--;
415+
break;
416+
417+
default:
418+
break;
419+
}
420+
}
421+
}
422+
}
396423
return tokens_.at(n).token_type();
397424
}
398425

test/help/spectest-interp.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ options:
2222
--enable-tail-call Enable Tail-call support
2323
--enable-bulk-memory Enable Bulk-memory operations
2424
--enable-reference-types Enable Reference types (anyref)
25+
--enable-annotations Enable Custom annotation syntax
2526
-V, --value-stack-size=SIZE Size in elements of the value stack
2627
-C, --call-stack-size=SIZE Size in elements of the call stack
2728
-t, --trace Trace execution

test/help/wasm-interp.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ options:
3333
--enable-tail-call Enable Tail-call support
3434
--enable-bulk-memory Enable Bulk-memory operations
3535
--enable-reference-types Enable Reference types (anyref)
36+
--enable-annotations Enable Custom annotation syntax
3637
-V, --value-stack-size=SIZE Size in elements of the value stack
3738
-C, --call-stack-size=SIZE Size in elements of the call stack
3839
-t, --trace Trace execution

test/help/wasm-validate.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ options:
2222
--enable-tail-call Enable Tail-call support
2323
--enable-bulk-memory Enable Bulk-memory operations
2424
--enable-reference-types Enable Reference types (anyref)
25+
--enable-annotations Enable Custom annotation syntax
2526
--no-debug-names Ignore debug names in the binary file
2627
--ignore-custom-section-errors Ignore errors in custom sections
2728
;;; STDOUT ;;)

test/help/wasm2wat.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ options:
2828
--enable-tail-call Enable Tail-call support
2929
--enable-bulk-memory Enable Bulk-memory operations
3030
--enable-reference-types Enable Reference types (anyref)
31+
--enable-annotations Enable Custom annotation syntax
3132
--inline-exports Write all exports inline
3233
--inline-imports Write all imports inline
3334
--no-debug-names Ignore debug names in the binary file

test/help/wast2json.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ options:
2525
--enable-tail-call Enable Tail-call support
2626
--enable-bulk-memory Enable Bulk-memory operations
2727
--enable-reference-types Enable Reference types (anyref)
28+
--enable-annotations Enable Custom annotation syntax
2829
-o, --output=FILE output wasm binary file
2930
-r, --relocatable Create a relocatable wasm binary (suitable for linking with e.g. lld)
3031
--no-canonicalize-leb128s Write all LEB128 sizes as 5-bytes instead of their minimal size

test/help/wat-desugar.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@ options:
3232
--enable-tail-call Enable Tail-call support
3333
--enable-bulk-memory Enable Bulk-memory operations
3434
--enable-reference-types Enable Reference types (anyref)
35+
--enable-annotations Enable Custom annotation syntax
3536
--generate-names Give auto-generated names to non-named functions, types, etc.
3637
;;; STDOUT ;;)

test/help/wat2wasm.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ options:
3232
--enable-tail-call Enable Tail-call support
3333
--enable-bulk-memory Enable Bulk-memory operations
3434
--enable-reference-types Enable Reference types (anyref)
35+
--enable-annotations Enable Custom annotation syntax
3536
-o, --output=FILE output wasm binary file
3637
-r, --relocatable Create a relocatable wasm binary (suitable for linking with e.g. lld)
3738
--no-canonicalize-leb128s Write all LEB128 sizes as 5-bytes instead of their minimal size

test/parse/annotations.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
;;; TOOL: wat2wasm
2+
;;; ARGS: --enable-annotations
3+
(module
4+
(func (@name "some func") (result i32)
5+
i32.const 42
6+
return)
7+
(@custom section)
8+
(@custom (@nested section))
9+
(@custom (section) (@with "other") nested-subsections))

test/parse/bad-annotations.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
;;; TOOL: wat2wasm
2+
;;; ERROR: 1
3+
(module
4+
(func (@name "some func") (result i32)
5+
i32.const 42
6+
return)
7+
(@custom section)
8+
(@custom (@nested section))
9+
(@custom (section) (@with "other") nested-subsections))
10+
(;; STDERR ;;;
11+
out/test/parse/bad-annotations.txt:4:9: error: annotations not enabled: name
12+
(func (@name "some func") (result i32)
13+
^^^^^^
14+
out/test/parse/bad-annotations.txt:4:16: error: unexpected token "some func", expected ).
15+
(func (@name "some func") (result i32)
16+
^^^^^^^^^^^
17+
out/test/parse/bad-annotations.txt:7:3: error: annotations not enabled: custom
18+
(@custom section)
19+
^^^^^^^^
20+
out/test/parse/bad-annotations.txt:7:12: error: unexpected token section.
21+
(@custom section)
22+
^^^^^^^
23+
;;; STDERR ;;)

0 commit comments

Comments
 (0)