Skip to content

Initial custom annotation support #1076

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/feature.def
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ WABT_FEATURE(multi_value, "multi-value", false, "Multi-value"
WABT_FEATURE(tail_call, "tail-call", false, "Tail-call support")
WABT_FEATURE(bulk_memory, "bulk-memory", false, "Bulk-memory operations")
WABT_FEATURE(reference_types, "reference-types", false, "Reference types (anyref)")
WABT_FEATURE(annotations, "annotations", false, "Custom annotation syntax")
1 change: 1 addition & 0 deletions src/token.def
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ WABT_TOKEN_LAST(Opcode, Unreachable)

/* Tokens with string data. */
WABT_TOKEN(AlignEqNat, "align=")
WABT_TOKEN(LparAnn, "Annotation")
WABT_TOKEN(OffsetEqNat, "offset=")
WABT_TOKEN(Reserved, "Reserved")
WABT_TOKEN(Text, "TEXT")
Expand Down
4 changes: 4 additions & 0 deletions src/wast-lexer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ Token WastLexer::GetToken(WastParser* parser) {
continue;
}
return BareToken(TokenType::Eof);
} else if (MatchString("(@")) {
ReadReservedChars();
// offset=2 to skip the "(@" prefix
return TextToken(TokenType::LparAnn, 2);
} else {
ReadChar();
return BareToken(TokenType::Lpar);
Expand Down
31 changes: 29 additions & 2 deletions src/wast-parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,35 @@ Location WastParser::GetLocation() {
}

TokenType WastParser::Peek(size_t n) {
while (tokens_.size() <= n)
tokens_.push_back(lexer_->GetToken(this));
while (tokens_.size() <= n) {
Token cur = lexer_->GetToken(this);
if (cur.token_type() != TokenType::LparAnn) {
tokens_.push_back(cur);
} else {
// Custom annotation. For now, discard until matching Rpar
if (!options_->features.annotations_enabled()) {
Error(cur.loc, "annotations not enabled: %s", cur.to_string().c_str());
return TokenType::Invalid;
}
int indent = 1;
while (indent > 0) {
cur = lexer_->GetToken(this);
switch (cur.token_type()) {
case TokenType::Lpar:
case TokenType::LparAnn:
indent++;
break;

case TokenType::Rpar:
indent--;
break;

default:
break;
}
}
}
}
return tokens_.at(n).token_type();
}

Expand Down
1 change: 1 addition & 0 deletions test/help/spectest-interp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ options:
--enable-tail-call Enable Tail-call support
--enable-bulk-memory Enable Bulk-memory operations
--enable-reference-types Enable Reference types (anyref)
--enable-annotations Enable Custom annotation syntax
-V, --value-stack-size=SIZE Size in elements of the value stack
-C, --call-stack-size=SIZE Size in elements of the call stack
-t, --trace Trace execution
Expand Down
1 change: 1 addition & 0 deletions test/help/wasm-interp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ options:
--enable-tail-call Enable Tail-call support
--enable-bulk-memory Enable Bulk-memory operations
--enable-reference-types Enable Reference types (anyref)
--enable-annotations Enable Custom annotation syntax
-V, --value-stack-size=SIZE Size in elements of the value stack
-C, --call-stack-size=SIZE Size in elements of the call stack
-t, --trace Trace execution
Expand Down
1 change: 1 addition & 0 deletions test/help/wasm-validate.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ options:
--enable-tail-call Enable Tail-call support
--enable-bulk-memory Enable Bulk-memory operations
--enable-reference-types Enable Reference types (anyref)
--enable-annotations Enable Custom annotation syntax
--no-debug-names Ignore debug names in the binary file
--ignore-custom-section-errors Ignore errors in custom sections
;;; STDOUT ;;)
1 change: 1 addition & 0 deletions test/help/wasm2wat.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ options:
--enable-tail-call Enable Tail-call support
--enable-bulk-memory Enable Bulk-memory operations
--enable-reference-types Enable Reference types (anyref)
--enable-annotations Enable Custom annotation syntax
--inline-exports Write all exports inline
--inline-imports Write all imports inline
--no-debug-names Ignore debug names in the binary file
Expand Down
1 change: 1 addition & 0 deletions test/help/wast2json.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ options:
--enable-tail-call Enable Tail-call support
--enable-bulk-memory Enable Bulk-memory operations
--enable-reference-types Enable Reference types (anyref)
--enable-annotations Enable Custom annotation syntax
-o, --output=FILE output wasm binary file
-r, --relocatable Create a relocatable wasm binary (suitable for linking with e.g. lld)
--no-canonicalize-leb128s Write all LEB128 sizes as 5-bytes instead of their minimal size
Expand Down
1 change: 1 addition & 0 deletions test/help/wat-desugar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ options:
--enable-tail-call Enable Tail-call support
--enable-bulk-memory Enable Bulk-memory operations
--enable-reference-types Enable Reference types (anyref)
--enable-annotations Enable Custom annotation syntax
--generate-names Give auto-generated names to non-named functions, types, etc.
;;; STDOUT ;;)
1 change: 1 addition & 0 deletions test/help/wat2wasm.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ options:
--enable-tail-call Enable Tail-call support
--enable-bulk-memory Enable Bulk-memory operations
--enable-reference-types Enable Reference types (anyref)
--enable-annotations Enable Custom annotation syntax
-o, --output=FILE output wasm binary file
-r, --relocatable Create a relocatable wasm binary (suitable for linking with e.g. lld)
--no-canonicalize-leb128s Write all LEB128 sizes as 5-bytes instead of their minimal size
Expand Down
9 changes: 9 additions & 0 deletions test/parse/annotations.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
;;; TOOL: wat2wasm
;;; ARGS: --enable-annotations
(module
(func (@name "some func") (result i32)
i32.const 42
return)
(@custom section)
(@custom (@nested section))
(@custom (section) (@with "other") nested-subsections))
23 changes: 23 additions & 0 deletions test/parse/bad-annotations.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
;;; TOOL: wat2wasm
;;; ERROR: 1
(module
(func (@name "some func") (result i32)
i32.const 42
return)
(@custom section)
(@custom (@nested section))
(@custom (section) (@with "other") nested-subsections))
(;; STDERR ;;;
out/test/parse/bad-annotations.txt:4:9: error: annotations not enabled: name
(func (@name "some func") (result i32)
^^^^^^
out/test/parse/bad-annotations.txt:4:16: error: unexpected token "some func", expected ).
(func (@name "some func") (result i32)
^^^^^^^^^^^
out/test/parse/bad-annotations.txt:7:3: error: annotations not enabled: custom
(@custom section)
^^^^^^^^
out/test/parse/bad-annotations.txt:7:12: error: unexpected token section.
(@custom section)
^^^^^^^
;;; STDERR ;;)