Skip to content
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

Add support for parsing async iterable<T> type #85

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ def test_difference(input, output):
typedef foo [ ] [ ] barTypes;
typedef sequence<DOMString[]> sequins;
typedef sequence<DOMString[]>? sequinses;
typedef async iterable<DOMString[]> asynciterables;
typedef async iterable<DOMString[]>? asynciterableses;
typedef object obj;
typedef (short or [Extended] double) union;
typedef (short or sequence < DOMString [ ] ? [ ] > ? or DOMString[]?[] or unsigned long long or unrestricted double) craziness;
Expand Down
29 changes: 28 additions & 1 deletion widlparser/productions.py
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,8 @@ class NonAnyType(ComplexProduction):
Syntax:
PrimitiveType [TypeSuffix] | "ByteString" [TypeSuffix] | "DOMString" [TypeSuffix]
| "USVString" TypeSuffix | Identifier [TypeSuffix] | "sequence" "<" TypeWithExtendedAttributes ">" [Null]
| "object" [TypeSuffix] | "Error" TypeSuffix | "Promise" "<" Type ">" [Null] | BufferRelatedType [Null]
| "async" "iterable" "<" TypeWithExtendedAttributes ">" [Null] | "object" [TypeSuffix] | "Error" TypeSuffix
| "Promise" "<" Type ">" [Null] | BufferRelatedType [Null]
| "FrozenArray" "<" TypeWithExtendedAttributes ">" [Null] | "ObservableArray" "<" TypeWithExtendedAttributes ">" [Null]
| "record" "<" StringType "," TypeWithExtendedAttributes ">"
"""
Expand All @@ -945,6 +946,7 @@ class NonAnyType(ComplexProduction):
type: (PrimitiveType | TypeIdentifier | TypeWithExtendedAttributes | Type | Symbol)
type_name: (str | None)
sequence: (Symbol | None)
async_iterable: (tuple[Symbol, Symbol] | None)
promise: (Symbol | None)
record: (Symbol | None)
_open_type: (Symbol | None)
Expand All @@ -968,6 +970,13 @@ def peek(cls, tokens: Tokenizer) -> bool:
if (Symbol.peek(tokens, '>')):
Symbol.peek(tokens, '?')
return tokens.pop_position(True)
elif (token and token.is_symbol('async')):
if (Symbol.peek(tokens, 'iterable')):
if (Symbol.peek(tokens, '<')):
if (TypeWithExtendedAttributes.peek(tokens)):
if (Symbol.peek(tokens, '>')):
Symbol.peek(tokens, '?')
return tokens.pop_position(True)
elif (token and token.is_symbol('Promise')):
if (Symbol.peek(tokens, '<')):
if (Type.peek(tokens)):
Expand All @@ -990,6 +999,7 @@ def peek(cls, tokens: Tokenizer) -> bool:
def __init__(self, tokens: Tokenizer, parent: ComplexProduction) -> None:
super().__init__(tokens, parent)
self.sequence = None
self.async_iterable = None
self.promise = None
self.record = None
self._open_type = None
Expand All @@ -1013,6 +1023,12 @@ def __init__(self, tokens: Tokenizer, parent: ComplexProduction) -> None:
self.type = TypeWithExtendedAttributes(tokens, self)
self._close_type = Symbol(tokens, '>', False)
self.null = Symbol(tokens, '?', False) if (Symbol.peek(tokens, '?')) else None
elif (token.is_symbol('async')):
self.async_iterable = (Symbol(tokens), Symbol(tokens))
self._open_type = Symbol(tokens, '<')
self.type = TypeWithExtendedAttributes(tokens, self)
self._close_type = Symbol(tokens, '>', False)
self.null = Symbol(tokens, '?', False) if (Symbol.peek(tokens, '?')) else None
elif (token.is_symbol('Promise')):
self.promise = Symbol(tokens, 'Promise')
self._open_type = Symbol(tokens, '<')
Expand Down Expand Up @@ -1043,6 +1059,9 @@ def _str(self) -> str:
if (self.sequence):
output = str(self.sequence) + str(self._open_type) + str(self.type) + str(self._close_type)
return output + (str(self.null) if (self.null) else '')
if (self.async_iterable):
output = str(self.async_iterable[0]) + str(self.async_iterable[1]) + str(self._open_type) + str(self.type) + str(self._close_type)
return output + (str(self.null) if (self.null) else '')
if (self.promise):
output = str(self.promise) + str(self._open_type) + str(self.type) + str(self._close_type)
return output + (str(self.null) if (self.null) else '')
Expand All @@ -1062,6 +1081,14 @@ def _define_markup(self, generator: MarkupGenerator) -> Production:
generator.add_text(self._close_type)
generator.add_text(self.null)
return self
if (self.async_iterable):
self.async_iterable[0].define_markup(generator)
self.async_iterable[1].define_markup(generator)
generator.add_text(self._open_type)
self.type.define_markup(generator)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.type.define_markup(generator)
generator.add_type(self.type)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I had copied this from the self.record branch.

generator.add_text(self._close_type)
generator.add_text(self.null)
return self
if (self.promise):
self.promise.define_markup(generator)
generator.add_text(self._open_type)
Expand Down