Skip to content

Commit 8497f2d

Browse files
authored
Merge pull request stadelmanma#51 from oponkork/master
Create unnamed nodes for keywords
2 parents 2756588 + 5535384 commit 8497f2d

File tree

5 files changed

+48905
-33662
lines changed

5 files changed

+48905
-33662
lines changed

grammar.js

+33-22
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ module.exports = grammar({
271271
commaSep1($.implicit_range),
272272
')'
273273
)),
274-
alias(caseInsensitive('none'), $.none)
274+
alias(caseInsensitive('none',aliasAsWord=false), $.none)
275275
)
276276
),
277277

@@ -409,9 +409,9 @@ module.exports = grammar({
409409
caseInsensitive('byte'),
410410
caseInsensitive('integer'),
411411
caseInsensitive('real'),
412-
caseInsensitive('double[ \t]*precision'),
412+
whiteSpacedKeyword('double','precision'),
413413
caseInsensitive('complex'),
414-
caseInsensitive('double[ \t]*complex'),
414+
whiteSpacedKeyword('double','complex'),
415415
caseInsensitive('logical'),
416416
caseInsensitive('character')
417417
),
@@ -444,7 +444,7 @@ module.exports = grammar({
444444
choice(
445445
caseInsensitive('in'),
446446
caseInsensitive('out'),
447-
caseInsensitive('in[ \t]*out')
447+
whiteSpacedKeyword('in','out')
448448
),
449449
')'
450450
),
@@ -558,7 +558,7 @@ module.exports = grammar({
558558
caseInsensitive('continue'),
559559
seq(caseInsensitive('cycle'), optional($.identifier)),
560560
seq(caseInsensitive('exit'), optional($.identifier)),
561-
seq(caseInsensitive('go[ \t]*to'), $.statement_label),
561+
seq(whiteSpacedKeyword('go','to'), $.statement_label),
562562
caseInsensitive('return'),
563563
seq(caseInsensitive('stop'), optional($._expression))
564564
),
@@ -578,7 +578,7 @@ module.exports = grammar({
578578
),
579579

580580
end_do_loop_statement: $ => seq(
581-
caseInsensitive('end[ \t]*do'),
581+
whiteSpacedKeyword('end','do'),
582582
optional($._block_label)
583583
),
584584

@@ -610,12 +610,12 @@ module.exports = grammar({
610610
),
611611

612612
end_if_statement: $ => seq(
613-
caseInsensitive('end[ \t]*if'),
613+
whiteSpacedKeyword('end', 'if'),
614614
optional($._block_label)
615615
),
616616

617617
elseif_clause: $ => seq(
618-
caseInsensitive('else[ \t]*if'),
618+
whiteSpacedKeyword('else','if'),
619619
$.parenthesized_expression,
620620
caseInsensitive('then'),
621621
optional($._block_label),
@@ -652,12 +652,12 @@ module.exports = grammar({
652652
),
653653

654654
end_where_statement: $ => seq(
655-
caseInsensitive('end[ \t]*where'),
655+
whiteSpacedKeyword('end','where'),
656656
optional($._block_label)
657657
),
658658

659659
elsewhere_clause: $ => seq(
660-
caseInsensitive('else[ \t]*where'),
660+
whiteSpacedKeyword('else','where'),
661661
optional($.parenthesized_expression),
662662
optional($._block_label),
663663
$._end_of_statement,
@@ -703,21 +703,21 @@ module.exports = grammar({
703703
),
704704

705705
end_forall_statement: $ => seq(
706-
caseInsensitive('end[ \t]*forall'),
706+
whiteSpacedKeyword('end','forall'),
707707
optional($._block_label)
708708
),
709709

710710
select_case_statement: $ => seq(
711711
optional($.block_label_start_expression),
712-
caseInsensitive('select[ \t]*case'),
712+
whiteSpacedKeyword('select','case'),
713713
$.selector,
714714
$._end_of_statement,
715715
repeat1($.case_statement),
716716
$.end_select_case_statement
717717
),
718718

719719
end_select_case_statement: $ => seq(
720-
caseInsensitive('end[ \t]*select'),
720+
whiteSpacedKeyword('end','select'),
721721
optional($._block_label)
722722
),
723723

@@ -821,7 +821,7 @@ module.exports = grammar({
821821

822822
explicit_enumerator: $ => seq($.identifier,'=', $.number_literal),
823823

824-
end_enum_statement: $=> caseInsensitive('end[ \t]*enum'),
824+
end_enum_statement: $=> whiteSpacedKeyword('end','enum'),
825825

826826
// precedence is used to override a conflict with the complex literal
827827
unit_identifier: $ => prec(1, choice(
@@ -1065,12 +1065,21 @@ module.exports = grammar({
10651065

10661066
module.exports.PREC = PREC
10671067

1068-
function caseInsensitive (keyword) {
1069-
return new RegExp(keyword
1068+
function caseInsensitive (keyword, aliasAsWord = true) {
1069+
let result = new RegExp(keyword
10701070
.split('')
10711071
.map(l => l !== l.toUpperCase() ? `[${l}${l.toUpperCase()}]` : l)
10721072
.join('')
10731073
)
1074+
if (aliasAsWord) result = alias(result, keyword)
1075+
return result
1076+
}
1077+
1078+
function whiteSpacedKeyword(prefix, suffix) {
1079+
return alias(choice(
1080+
seq(caseInsensitive(prefix,aliasAsWord= false), caseInsensitive(suffix,aliasAsWord= false)),
1081+
caseInsensitive(prefix + suffix, aliasAsWord= false)),
1082+
prefix + suffix)
10741083
}
10751084

10761085
/* TODO
@@ -1091,15 +1100,17 @@ function sep1 (rule, separator) {
10911100
return seq(rule, repeat(seq(separator, rule)))
10921101
}
10931102

1103+
// This can be merged with whiteSpacedKeyword, keeping for now.
10941104
function blockStructureEnding ($, structType) {
10951105
const obj = prec.right(seq(
1096-
caseInsensitive('end'),
1097-
optional(seq(
1098-
caseInsensitive(structType),
1099-
optional($.identifier)
1100-
)),
1106+
alias(choice(
1107+
seq(
1108+
caseInsensitive('end',aliasAsWord=false),
1109+
optional(caseInsensitive(structType,aliasAsWord=false))),
1110+
caseInsensitive('end'+structType,aliasAsWord=false)),
1111+
'end'+structType),
1112+
optional($.identifier),
11011113
$._end_of_statement
11021114
))
1103-
//
11041115
return obj
11051116
}

0 commit comments

Comments
 (0)