Skip to content

Commit 000ba5b

Browse files
committed
Remove HLL::Grammar.O(:inherit).
This functionality is removed in favour of lexical hashes flattened directly into invocations of <O(...)>.
1 parent 3105e6a commit 000ba5b

File tree

2 files changed

+82
-104
lines changed

2 files changed

+82
-104
lines changed

src/HLL/Grammar.nqp

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -130,60 +130,40 @@ grammar HLL::Grammar {
130130

131131
=begin
132132

133-
=item O($save, *%spec)
133+
=item O(*%spec)
134134
135135
This subrule attaches operator precedence information to
136136
a match object (such as an operator token). A typical
137137
invocation for the subrule might be:
138138

139-
token infix:sym<+> { <sym> <O(:inherit<%additive>, :op<add>)> }
139+
token infix:sym<+> { <sym> <O(|%additive, :op<add>)> }
140140

141-
This says to add all of the attribute of the C<%additive> hash
141+
This says to add all of the attributes of the C<%additive> hash
142142
(described below) and a C<op> entry into the match object
143143
returned by the C<< infix:sym<+> >> token (as the C<O> named
144144
capture). Note that this is a alphabetic "O", not a digit zero.
145145

146-
The C<save> argument is used to build "hash" aggregates that can
147-
be referred to by subsequent calls to C<O>. For example,
146+
The %additive hash is simply a hash containing information that is shared
147+
between all additive operators. Generally, this will simply be a normal
148+
lexically scoped hash belonging to the grammar. For example, the NQP grammar
149+
has:
148150

149-
NQP::Grammar.O('%additive', :prec<t=>, :assoc<left>);
150-
151-
specifies the values to be associated with later references to
152-
"%additive". Eventually it will likely be possible to use true
153-
hashes from a package namespace, but this works for now.
151+
grammar NQP::Grammar is HLL::Grammar {
152+
my %additive := nqp::hash('prec', 't=', 'assoc', 'left');
153+
token infix:sym<+> { <sym> <O(|%additive, :op<add_n>)> }
154+
}
154155

155156
=end
156157

157-
# This lexical holds the hash cache. Right now we have one
158-
# cache for all grammars; eventually we may need a way to
159-
# separate them out by cursor type.
160-
my %ohash;
161-
162-
method O($save?, *%spec) {
163-
my %hash;
164-
nqp::die("\%spec argument to HLL::Grammar.O must be non-empty.") if not %spec;
165-
166-
if %spec<inherit> {
167-
%hash := nqp::clone(%ohash{%spec<inherit>});
168-
nqp::deletekey(%spec, 'inherit');
169-
for %spec -> $key { %hash{$key} := %spec{$key}; }
170-
}
171-
else {
172-
%hash := %spec;
173-
}
174-
175-
if $save {
176-
%ohash{$save} := %hash;
177-
self;
178-
}
179-
else {
180-
# If we've been called as a subrule, then build a pass-cursor
181-
# to indicate success and set the hash as the subrule's match object.
182-
my $cur := self.'!cursor_start_cur'();
183-
$cur.'!cursor_pass'(nqp::getattr_i($cur, $cursor_class, '$!from'));
184-
nqp::bindattr($cur, $cursor_class, '$!match', %hash);
185-
$cur;
186-
}
158+
method O(*%spec) {
159+
nqp::die("HLL::Grammar.O called without any arguments") if not %spec;
160+
161+
# Build a pass-cursor to indicate success and set the hash as the
162+
# subrule's match object.
163+
my $cur := self.'!cursor_start_cur'();
164+
$cur.'!cursor_pass'(nqp::getattr_i($cur, $cursor_class, '$!from'));
165+
nqp::bindattr($cur, $cursor_class, '$!match', %spec);
166+
$cur;
187167
}
188168

189169

src/NQP/Grammar.nqp

Lines changed: 62 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -697,23 +697,21 @@ grammar NQP::Grammar is HLL::Grammar {
697697

698698
## Operators
699699

700-
INIT {
701-
NQP::Grammar.O('%methodop', :prec<y=>, :assoc<unary>);
702-
NQP::Grammar.O('%autoincrement', :prec<x=>, :assoc<unary>);
703-
NQP::Grammar.O('%exponentiation', :prec<w=>, :assoc<left>);
704-
NQP::Grammar.O('%symbolic_unary', :prec<v=>, :assoc<unary>);
705-
NQP::Grammar.O('%multiplicative', :prec<u=>, :assoc<left>);
706-
NQP::Grammar.O('%additive', :prec<t=>, :assoc<left>);
707-
NQP::Grammar.O('%concatenation', :prec<r=>, :assoc<left>);
708-
NQP::Grammar.O('%relational', :prec<m=>, :assoc<non>);
709-
NQP::Grammar.O('%tight_and', :prec<l=>, :assoc<left>);
710-
NQP::Grammar.O('%tight_or', :prec<k=>, :assoc<left>);
711-
NQP::Grammar.O('%conditional', :prec<j=>, :assoc<right>);
712-
NQP::Grammar.O('%assignment', :prec<i=>, :assoc<right>);
713-
NQP::Grammar.O('%comma', :prec<g=>, :assoc<list>, :nextterm<nulltermish>);
714-
NQP::Grammar.O('%list_infix', :prec<f=>, :assoc<list>);
715-
NQP::Grammar.O('%list_prefix', :prec<e=>, :assoc<unary>);
716-
}
700+
my %methodop := nqp::hash('prec', 'y=', 'assoc', 'unary');
701+
my %autoincrement := nqp::hash('prec', 'x=', 'assoc', 'unary');
702+
my %exponentiation := nqp::hash('prec', 'w=', 'assoc', 'left');
703+
my %symbolic_unary := nqp::hash('prec', 'v=', 'assoc', 'unary');
704+
my %multiplicative := nqp::hash('prec', 'u=', 'assoc', 'left');
705+
my %additive := nqp::hash('prec', 't=', 'assoc', 'left');
706+
my %concatenation := nqp::hash('prec', 'r=', 'assoc', 'left');
707+
my %relational := nqp::hash('prec', 'm=', 'assoc', 'non');
708+
my %tight_and := nqp::hash('prec', 'l=', 'assoc', 'left');
709+
my %tight_or := nqp::hash('prec', 'k=', 'assoc', 'left');
710+
my %conditional := nqp::hash('prec', 'j=', 'assoc', 'right');
711+
my %assignment := nqp::hash('prec', 'i=', 'assoc', 'right');
712+
my %comma := nqp::hash('prec', 'g=', 'assoc', 'list', 'nextterm', 'nulltermish');
713+
my %list_infix := nqp::hash('prec', 'f=', 'assoc', 'list');
714+
my %list_prefix := nqp::hash('prec', 'e=', 'assoc', 'unary');
717715

718716

719717
token infixish { <!infixstopper> <OPER=infix> }
@@ -724,90 +722,90 @@ grammar NQP::Grammar is HLL::Grammar {
724722

725723
token postcircumfix:sym<[ ]> {
726724
'[' <.ws> <EXPR> ']'
727-
<O(:inherit<%methodop>)>
725+
<O(|%methodop)>
728726
}
729727

730728
token postcircumfix:sym<{ }> {
731729
'{' <.ws> <EXPR> '}'
732-
<O(:inherit<%methodop>)>
730+
<O(|%methodop)>
733731
}
734732

735733
token postcircumfix:sym<ang> {
736734
<?[<]> <quote_EXPR: ':q'>
737-
<O(:inherit<%methodop>)>
735+
<O(|%methodop)>
738736
}
739737

740738
token postcircumfix:sym<( )> {
741739
'(' <.ws> <arglist> ')'
742-
<O(:inherit<%methodop>)>
740+
<O(|%methodop)>
743741
}
744742

745-
token postfix:sym<.> { <dotty> <O(:inherit<%methodop>)> }
743+
token postfix:sym<.> { <dotty> <O(|%methodop)> }
746744

747-
token prefix:sym<++> { <sym> <O(:inherit<%autoincrement>, :op<preinc>)> }
748-
token prefix:sym<--> { <sym> <O(:inherit<%autoincrement>, :op<predec>)> }
745+
token prefix:sym<++> { <sym> <O(|%autoincrement, :op<preinc>)> }
746+
token prefix:sym<--> { <sym> <O(|%autoincrement, :op<predec>)> }
749747

750-
token postfix:sym<++> { <sym> <O(:inherit<%autoincrement>, :op<postinc>)> }
751-
token postfix:sym<--> { <sym> <O(:inherit<%autoincrement>, :op<postdec>)> }
748+
token postfix:sym<++> { <sym> <O(|%autoincrement, :op<postinc>)> }
749+
token postfix:sym<--> { <sym> <O(|%autoincrement, :op<postdec>)> }
752750

753-
token infix:sym<**> { <sym> <O(:inherit<%exponentiation>, :op<pow_n>)> }
751+
token infix:sym<**> { <sym> <O(|%exponentiation, :op<pow_n>)> }
754752

755-
token prefix:sym<+> { <sym> <O(:inherit<%symbolic_unary>, :op<numify>)> }
756-
token prefix:sym<~> { <sym> <O(:inherit<%symbolic_unary>, :op<stringify>)> }
757-
token prefix:sym<-> { <sym> <![>]> <!number> <O(:inherit<%symbolic_unary>, :op<neg_n>)> }
758-
token prefix:sym<?> { <sym> <O(:inherit<%symbolic_unary>, :op<istrue>)> }
759-
token prefix:sym<!> { <sym> <O(:inherit<%symbolic_unary>, :op<falsey>)> }
760-
token prefix:sym<|> { <sym> <O(:inherit<%symbolic_unary>)> }
753+
token prefix:sym<+> { <sym> <O(|%symbolic_unary, :op<numify>)> }
754+
token prefix:sym<~> { <sym> <O(|%symbolic_unary, :op<stringify>)> }
755+
token prefix:sym<-> { <sym> <![>]> <!number> <O(|%symbolic_unary, :op<neg_n>)> }
756+
token prefix:sym<?> { <sym> <O(|%symbolic_unary, :op<istrue>)> }
757+
token prefix:sym<!> { <sym> <O(|%symbolic_unary, :op<falsey>)> }
758+
token prefix:sym<|> { <sym> <O(|%symbolic_unary)> }
761759

762-
token infix:sym<*> { <sym> <O(:inherit<%multiplicative>, :op<mul_n>)> }
763-
token infix:sym</> { <sym> <O(:inherit<%multiplicative>, :op<div_n>)> }
764-
token infix:sym<%> { <sym> <O(:inherit<%multiplicative>, :op<mod_n>)> }
765-
token infix:sym<+&> { <sym> <O(:inherit<%multiplicative>, :op<bitand_i>)> }
760+
token infix:sym<*> { <sym> <O(|%multiplicative, :op<mul_n>)> }
761+
token infix:sym</> { <sym> <O(|%multiplicative, :op<div_n>)> }
762+
token infix:sym<%> { <sym> <O(|%multiplicative, :op<mod_n>)> }
763+
token infix:sym<+&> { <sym> <O(|%multiplicative, :op<bitand_i>)> }
766764

767-
token infix:sym<+> { <sym> <O(:inherit<%additive>, :op<add_n>)> }
768-
token infix:sym<-> { <sym> <O(:inherit<%additive>, :op<sub_n>)> }
769-
token infix:sym<+|> { <sym> <O(:inherit<%additive>, :op<bitor_i>)> }
770-
token infix:sym<+^> { <sym> <O(:inherit<%additive>, :op<bitxor_i>)> }
765+
token infix:sym<+> { <sym> <O(|%additive, :op<add_n>)> }
766+
token infix:sym<-> { <sym> <O(|%additive, :op<sub_n>)> }
767+
token infix:sym<+|> { <sym> <O(|%additive, :op<bitor_i>)> }
768+
token infix:sym<+^> { <sym> <O(|%additive, :op<bitxor_i>)> }
771769

772-
token infix:sym<~> { <sym> <O(:inherit<%concatenation>, :op<concat>)> }
770+
token infix:sym<~> { <sym> <O(|%concatenation, :op<concat>)> }
773771

774-
token infix:sym«==» { <sym> <O(:inherit<%relational>, :op<iseq_n>)> }
775-
token infix:sym«!=» { <sym> <O(:inherit<%relational>, :op<isne_n>)> }
776-
token infix:sym«<=» { <sym> <O(:inherit<%relational>, :op<isle_n>)> }
777-
token infix:sym«>=» { <sym> <O(:inherit<%relational>, :op<isge_n>)> }
778-
token infix:sym«<» { <sym> <O(:inherit<%relational>, :op<islt_n>)> }
779-
token infix:sym«>» { <sym> <O(:inherit<%relational>, :op<isgt_n>)> }
780-
token infix:sym«eq» { <sym> <O(:inherit<%relational>, :op<iseq_s>)> }
781-
token infix:sym«ne» { <sym> <O(:inherit<%relational>, :op<isne_s>)> }
782-
token infix:sym«le» { <sym> <O(:inherit<%relational>, :op<isle_s>)> }
783-
token infix:sym«ge» { <sym> <O(:inherit<%relational>, :op<isge_s>)> }
784-
token infix:sym«lt» { <sym> <O(:inherit<%relational>, :op<islt_s>)> }
785-
token infix:sym«gt» { <sym> <O(:inherit<%relational>, :op<isgt_s>)> }
786-
token infix:sym«=:=» { <sym> <O(:inherit<%relational>, :op<eqaddr>)> }
787-
token infix:sym<~~> { <sym> <O(:inherit<%relational>, :reducecheck<smartmatch>)> }
772+
token infix:sym«==» { <sym> <O(|%relational, :op<iseq_n>)> }
773+
token infix:sym«!=» { <sym> <O(|%relational, :op<isne_n>)> }
774+
token infix:sym«<=» { <sym> <O(|%relational, :op<isle_n>)> }
775+
token infix:sym«>=» { <sym> <O(|%relational, :op<isge_n>)> }
776+
token infix:sym«<» { <sym> <O(|%relational, :op<islt_n>)> }
777+
token infix:sym«>» { <sym> <O(|%relational, :op<isgt_n>)> }
778+
token infix:sym«eq» { <sym> <O(|%relational, :op<iseq_s>)> }
779+
token infix:sym«ne» { <sym> <O(|%relational, :op<isne_s>)> }
780+
token infix:sym«le» { <sym> <O(|%relational, :op<isle_s>)> }
781+
token infix:sym«ge» { <sym> <O(|%relational, :op<isge_s>)> }
782+
token infix:sym«lt» { <sym> <O(|%relational, :op<islt_s>)> }
783+
token infix:sym«gt» { <sym> <O(|%relational, :op<isgt_s>)> }
784+
token infix:sym«=:=» { <sym> <O(|%relational, :op<eqaddr>)> }
785+
token infix:sym<~~> { <sym> <O(|%relational, :reducecheck<smartmatch>)> }
788786

789-
token infix:sym<&&> { <sym> <O(:inherit<%tight_and>, :op<if>)> }
787+
token infix:sym<&&> { <sym> <O(|%tight_and, :op<if>)> }
790788

791-
token infix:sym<||> { <sym> <O(:inherit<%tight_or>, :op<unless>)> }
792-
token infix:sym<//> { <sym> <O(:inherit<%tight_or>, :op<defor>)> }
789+
token infix:sym<||> { <sym> <O(|%tight_or, :op<unless>)> }
790+
token infix:sym<//> { <sym> <O(|%tight_or, :op<defor>)> }
793791

794792
token infix:sym<?? !!> {
795793
'??'
796794
<.ws>
797795
<EXPR('i=')>
798796
'!!'
799-
<O(:inherit<%conditional>, :reducecheck<ternary>, :op<if>)>
797+
<O(|%conditional, :reducecheck<ternary>, :op<if>)>
800798
}
801799

802800
token infix:sym<=> {
803801
<sym> <.panic: 'Assignment ("=") not supported in NQP, use ":=" instead'>
804802
}
805-
token infix:sym<:=> { <sym> <O(:inherit<%assignment>, :op<bind>)> }
806-
token infix:sym<::=> { <sym> <O(:inherit<%assignment>, :op<bind>)> }
803+
token infix:sym<:=> { <sym> <O(|%assignment, :op<bind>)> }
804+
token infix:sym<::=> { <sym> <O(|%assignment, :op<bind>)> }
807805

808-
token infix:sym<,> { <sym> <O(:inherit<%comma>, :op<list>)> }
806+
token infix:sym<,> { <sym> <O(|%comma, :op<list>)> }
809807

810-
token prefix:sym<make> { <sym> \s <O(:inherit<%list_prefix>)> }
808+
token prefix:sym<make> { <sym> \s <O(|%list_prefix)> }
811809
token term:sym<return> { <sym> [\s <EXPR>]? { $*RETURN_USED := 1 } }
812810

813811
method smartmatch($/) {

0 commit comments

Comments
 (0)