Skip to content

Instantly share code, notes, and snippets.

@bjconlan
Last active September 4, 2020 12:43
Show Gist options
  • Save bjconlan/68d7b7498d7ac9c02dd6712ae6b1c8ab to your computer and use it in GitHub Desktop.
Save bjconlan/68d7b7498d7ac9c02dd6712ae6b1c8ab to your computer and use it in GitHub Desktop.
WebIDL antlr grammar - W3C Editor's Draft 17 August 2020
grammar WebIDL;
file
: definitions* EOF
;
definitions
: extendedAttributeList? definition
;
definition
: callbackOrInterfaceOrMixin
| namespace
| partial
| dictionary
| enum_
| typedef_
| includesStatment
;
extendedAttributeList
: '[' extendedAttribute (',' extendedAttribute)* ']'
;
extendedAttribute
: IDENTIFIER extendedAttributeArgs?
;
extendedAttributeArgs
: '(' argumentList ')'
| '=' IDENTIFIER
| '=' '(' identifierList ')'
| '=' IDENTIFIER '(' argumentList ')'
;
identifierList
: IDENTIFIER (',' IDENTIFIER)*
;
callbackOrInterfaceOrMixin
: 'callback' callbackRestOrInterface
| 'interface' interfaceOrMixin
;
interfaceOrMixin
: IDENTIFIER inheritance? '{' interfaceMembers* '}' ';'
| mixinRest
;
partial
: 'partial' partialDefinition
;
partialDefinition
: 'interface' partialInterfaceOrPartialMixin
| partialDictionary
| namespace
;
partialInterfaceOrPartialMixin
: IDENTIFIER '{' partialInterfaceMembers* '}' ';'
| mixinRest
;
interfaceMembers
: extendedAttributeList? interfaceMember
;
interfaceMember
: partialInterfaceMember
| constructor
;
partialInterfaceMembers
: extendedAttributeList? partialInterfaceMember
;
partialInterfaceMember
: const_
| operation
| stringifier
| staticMember
| iterable
| asyncInterable
| readOnlyMember
| readWriteAttribute
| readWriteMaplike
| readWriteSetlike
| inheritAttribute
;
inheritance
: ':' IDENTIFIER
;
namespace
: 'namespace' IDENTIFIER '{' namespaceMembers* '}' ';'
;
namespaceMembers
: extendedAttributeList? namespaceMember
;
namespaceMember
: regularOperation
| 'readonly' attributeRest
;
dictionary
: 'dictionary' IDENTIFIER inheritance? '{' dictionaryMember* '}' ';'
;
dictionaryMember
: extendedAttributeList? dictionaryMemberRest
;
dictionaryMemberRest
: 'required' typeWithExtendedAttributes IDENTIFIER ';'
| type IDENTIFIER default_? ';'
;
partialDictionary
: 'dictionary' IDENTIFIER '{' dictionaryMember* '}' ';'
;
default_
: '=' defaultValue
;
defaultValue
: constValue
| STRING
| '[' ']'
| '{' '}'
| 'null'
;
enum_
: 'enum' IDENTIFIER '{' enumValueList '}' ';'
;
enumValueList
: STRING (',' STRING)*
;
typedef_
: 'typedef' typeWithExtendedAttributes IDENTIFIER ';'
;
includesStatment
: IDENTIFIER 'includes' IDENTIFIER ';'
;
argumentNameKeyword
: 'async'
| 'attribute'
| 'callback'
| 'const'
| 'constructor'
| 'deleter'
| 'dictionary'
| 'enum'
| 'getter'
| 'includes'
| 'inherit'
| 'interface'
| 'iterable'
| 'maplike'
| 'mixin'
| 'namespace'
| 'partial'
| 'readonly'
| 'required'
| 'setlike'
| 'setter'
| 'static'
| 'stringifier'
| 'typedef'
| 'unrestricted'
;
mixinRest
: 'mixin' IDENTIFIER '{' mixinMembers* '}' ';'
;
mixinMembers
: extendedAttributeList? mixinMember
;
mixinMember
: const_
| regularOperation
| stringifier
| 'readonly'? attributeRest
;
callbackRestOrInterface
: callbackRest
| 'interface' IDENTIFIER '{' callbackInterfaceMembers* '}' ';'
;
callbackInterfaceMembers
: extendedAttributeList? callbackInterfaceMember
;
callbackInterfaceMember
: const_
| regularOperation
;
const_
: 'const' constType IDENTIFIER '=' constValue ';'
;
constValue
: booleanLiteral
| floatLiteral
| INTEGER
;
booleanLiteral
: 'true'
| 'false'
;
floatLiteral
: DECIMAL
| '-Infinity'
| 'Infinity'
| 'NaN'
;
constType
: primitiveType
| IDENTIFIER
;
readOnlyMember
: 'readonly' readOnlyMemberRest
;
readOnlyMemberRest
: attributeRest
| maplikeRest
| setlikeRest
;
readWriteAttribute
: attributeRest
;
inheritAttribute
: 'inherit' attributeRest
;
attributeRest
: 'attribute' typeWithExtendedAttributes attributeName ';'
;
attributeName
: attributeNameKeyword
| IDENTIFIER
;
attributeNameKeyword
: 'async'
| 'required'
;
operation
: regularOperation
| specialOperation
;
regularOperation
: type operationRest
;
specialOperation
: special regularOperation
;
special
: 'getter'
| 'setter'
| 'deleter'
;
operationRest
: operationName? '(' argumentList* ')' ';'
;
operationName
: operationNameKeyword
| IDENTIFIER
;
operationNameKeyword
: 'includes'
;
argumentList
: argument (',' argument)*
;
argument
: extendedAttributeList? argumentRest
;
argumentRest
: 'optional' typeWithExtendedAttributes argumentName default_?
| type '...'? argumentName
;
argumentName
: argumentNameKeyword
| IDENTIFIER
;
constructor
: 'constructor' '(' argumentList* ')' ';'
;
stringifier
: 'stringifier' stringifierRest
;
stringifierRest
: 'readonly'? attributeRest
| regularOperation
| ';'
;
staticMember
: 'static' staticMemberRest
;
staticMemberRest
: 'readonly'? attributeRest
| regularOperation
;
iterable
: 'interable' '<' typeWithExtendedAttributes (',' typeWithExtendedAttributes)* '>'
;
asyncInterable
: 'async' iterable argumentList?
;
readWriteMaplike
: maplikeRest
;
maplikeRest
: 'maplike' '<' typeWithExtendedAttributes ',' typeWithExtendedAttributes '>' ';'
;
readWriteSetlike
: setlikeRest
;
setlikeRest
: 'setlike' '<' typeWithExtendedAttributes '>' ';'
;
callbackRest
: IDENTIFIER '=' type '(' argumentList ')' ';'
;
type
: singleType
| unionType null_?
;
typeWithExtendedAttributes
: extendedAttributeList? type
;
singleType
: distinguishableType
| 'any'
| promiseType
;
unionType
: '(' unionMemberType ('or' unionMemberType)+ ')'
;
unionMemberType
: extendedAttributeList? distinguishableType
| unionType null_?
;
distinguishableType
: primitiveType null_?
| stringType null_?
| IDENTIFIER null_?
| 'sequence' '<' typeWithExtendedAttributes '>' null_?
| 'object' null_?
| 'symbol' null_?
| bufferRelatedType null_?
| 'FrozenArray' '<' typeWithExtendedAttributes '>' null_?
| 'ObservableArray' '<' typeWithExtendedAttributes '>' null_?
| recordType null_?
;
primitiveType
: unsignedIntegerType
| unrestrictedFloatType
| 'undefined'
| 'boolean'
| 'byte'
| 'octet'
;
unrestrictedFloatType
: 'unrestricted' floatType
| floatType
;
floatType
: 'float'
| 'double'
;
unsignedIntegerType
: 'unsigned' integerType
| integerType
;
integerType
: 'short'
| 'long' 'long'?
;
stringType
: 'ByteString'
| 'DOMString'
| 'USVString'
;
promiseType
: 'Promise' '<' type '>'
;
recordType
: 'record' '<' stringType ',' typeWithExtendedAttributes '>'
;
null_
: '?'
;
bufferRelatedType
: 'ArrayBuffer'
| 'DataView'
| 'Int8Array'
| 'Int16Array'
| 'Int32Array'
| 'Uint8Array'
| 'Uint16Array'
| 'Uint32Array'
| 'Uint8ClampedArray'
| 'Float32Array'
| 'Float64Array'
;
INTEGER: '-'?('0'([Xx][0-9A-Fa-f]+|[0-7]*)|[1-9][0-9]*);
DECIMAL: '-'?(([0-9]+'.'[0-9]*|[0-9]*'.'[0-9]+)([Ee][+\-]?[0-9]+)?|[0-9]+[Ee][+\-]?[0-9]+);
IDENTIFIER: [A-Z_a-z][0-9A-Z_a-z]*;
STRING: '"' ~["]* '"';
WHITESPACE: [\t\n\r ]+ -> skip;
COMMENT: ('/*' .*? '*/' | '//' ~ [\r\n]*) -> skip;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment