Skip to content

Commit 513ccfc

Browse files
committed
Pass one AST test
1 parent 76560a6 commit 513ccfc

File tree

5 files changed

+1292
-1345
lines changed

5 files changed

+1292
-1345
lines changed

bin/parse.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,12 @@
22
require_once __DIR__ . "/../vendor/autoload.php";
33

44
global $argv;
5-
var_dump( \Wikimedia\WebIDL\WebIDL::parse( file_get_contents( $argv[1] ) ) );
5+
6+
$filename = $argv[1];
7+
$options = [
8+
'sourceName' => $filename,
9+
'concrete' => true,
10+
];
11+
$ast = \Wikimedia\WebIDL\WebIDL::parse( file_get_contents( $filename ), $options );
12+
$json = json_encode( $ast, JSON_PRETTY_PRINT );
13+
echo( "$json\n" );

src/Grammar.pegphp

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace Wikimedia\WebIDL;
2424
}
2525
}
2626

27-
start = _ Definitions
27+
start = _ d:Definitions { return $d; }
2828

2929
/* Line number bookkeeping.
3030
* Be careful about backtracking after you successfully match this production.
@@ -53,7 +53,7 @@ identifier = !(ArgumentNameKeyword / BufferRelatedType / OtherIdLike)
5353
s:$( [-_]? [A-Za-z] [-_0-9A-Za-z]* )
5454
// weird special case reserved identifiers:
5555
&{ return $s !== "_constructor" && $s !== "_toString" && $s !== "toString"; }
56-
{ return $s; }
56+
{ return $s[0] == "_" ? substr($s, 1) : $s ; }
5757

5858
string = "\"" s:$([^\"]*) "\"" { return $s; }
5959

@@ -70,7 +70,10 @@ _ = ( whitespace / comment )*
7070
i_ = ![-_0-9A-Za-z] _
7171

7272
/* WebIDL non-terminals */
73-
Definitions = (ExtendedAttributeList Definition)*
73+
Definitions = (e:ExtendedAttributeList d:Definition {
74+
$d['extAttrs'] = $e;
75+
return $d;
76+
})*
7477

7578
Definition =
7679
CallbackOrInterfaceOrMixin
@@ -109,18 +112,27 @@ ArgumentNameKeyword =
109112
/ "unrestricted" ) i_
110113

111114
CallbackOrInterfaceOrMixin =
112-
"callback" i_ CallbackRestOrInterface
113-
/ "interface" i_ InterfaceOrMixin
115+
"callback" i_ CallbackRestOrInterface
116+
/ "interface" i_ im:InterfaceOrMixin {
117+
$im['type'] = "interface"; return $im;
118+
}
114119
InterfaceOrMixin = InterfaceRest / MixinRest
115120
InterfaceRest =
116-
identifier _ Inheritance "{" _ InterfaceMembers "}" _ ";" _
121+
name:identifier _ inh:Inheritance "{" _ m:InterfaceMembers "}" _ ";" _ {
122+
return [
123+
'name' => $name,
124+
'inheritance' => $inh,
125+
'members' => $m,
126+
'partial' => false,
127+
];
128+
}
117129
Partial = "partial" i_ PartialDefinition
118130
PartialDefinition =
119131
"interface" i_ PartialInterfaceOrPartialMixin
120132
/ PartialDictionary
121133
/ Namespace
122134
PartialInterfaceOrPartialMixin =
123-
PartialInterfaceRest
135+
PartialInterfaceRest
124136
/ MixinRest
125137
PartialInterfaceRest =
126138
identifier _ "{" _ PartialInterfaceMembers "}" _ ";" _
@@ -141,7 +153,9 @@ PartialInterfaceMember =
141153
/ ReadWriteMaplike
142154
/ ReadWriteSetlike
143155
/ InheritAttribute
144-
Inheritance = ( ":" _ identifier _ )?
156+
Inheritance =
157+
( ":" _ name:identifier _ ) { return $name; }
158+
/ "" { return null; }
145159
MixinRest = "mixin" i_ identifier _ "{" _ MixinMembers "}" _ ";" _
146160
MixinMembers = ( ExtendedAttributeList MixinMember )*
147161
MixinMember =
@@ -150,8 +164,14 @@ MixinMember =
150164
/ Stringifier
151165
/ OptionalReadOnly AttributeRest
152166

153-
IncludesStatement = identifier _ "includes" i_ identifier _ ";" _
154-
167+
IncludesStatement =
168+
target:identifier _ "includes" i_ incl:identifier _ ";" _ {
169+
return [
170+
'type' => 'includes',
171+
'target' => $target,
172+
'includes' => $incl,
173+
];
174+
}
155175
CallbackRestOrInterface =
156176
CallbackRest
157177
/ "interface" i_ identifier _ "{" _ CallbackInterfaceMembers "}" _ ";" _
@@ -247,7 +267,14 @@ SetlikeRest =
247267
"setlike" i_ "<" _ TypeWithExtendedAttributes ">" _ ";" _
248268

249269
Namespace =
250-
"namespace" i_ identifier _ "{" _ NamespaceMembers "}" _ ";" _
270+
"namespace" i_ name:identifier _ "{" _ m:NamespaceMembers "}" _ ";" _ {
271+
return [
272+
'type' => 'namespace',
273+
'name' => $name,
274+
'partial' => false,
275+
'members' => $m,
276+
];
277+
}
251278
NamespaceMembers = ( ExtendedAttributeList NamespaceMember )*
252279
NamespaceMember =
253280
RegularOperation
@@ -340,13 +367,18 @@ BufferRelatedType =
340367
/ "Float64Array" ) i_
341368

342369
ExtendedAttributeList =
343-
"[" _ ExtendedAttribute ExtendedAttributes "]" _ / !"[" ""
344-
ExtendedAttributes = "," _ ExtendedAttribute ExtendedAttributes / ""
370+
"[" _ e:$ExtendedAttribute rest:ExtendedAttributes "]" _
371+
{ array_unshift($rest, $e); return $rest; }
372+
/ !"[" "" { return []; }
373+
ExtendedAttributes =
374+
"," _ e:$ExtendedAttribute rest:ExtendedAttributes
375+
{ array_unshift($rest, $e); return $rest; }
376+
/ "" { return []; }
345377
ExtendedAttribute =
346378
"(" _ ExtendedAttributeInner ")" _ ExtendedAttributeRest
347379
/ "[" _ ExtendedAttributeInner "]" _ ExtendedAttributeRest
348380
/ "{" _ ExtendedAttributeInner "}" _ ExtendedAttributeRest
349-
/ Other ExtendedAttributeRest
381+
/ Other+ ExtendedAttributeRest
350382
ExtendedAttributeRest = ExtendedAttribute?
351383
ExtendedAttributeInner =
352384
"(" _ ExtendedAttributeInner ")" _ ExtendedAttributeInner

0 commit comments

Comments
 (0)