Skip to content

Commit 1f28d29

Browse files
committed
fix(parser): make whitespace optional between recognized parsers
1 parent d6b3ef0 commit 1f28d29

File tree

3 files changed

+141
-56
lines changed

3 files changed

+141
-56
lines changed

src/lib/auth/templates/language/compile.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ test('compileScript: clean errors on unexpected input', t => {
3030
errors: [
3131
{
3232
error:
33-
'Encountered unexpected input while parsing script. Expected whitespace or the end of the script.',
33+
"Encountered unexpected input while parsing script. Expected the opening parenthesis of this evaluation ('(').",
3434
range: {
35-
endColumn: 3,
35+
endColumn: 4,
3636
endLineNumber: 1,
37-
startColumn: 3,
37+
startColumn: 4,
3838
startLineNumber: 1
3939
}
4040
}

src/lib/auth/templates/language/parse.spec.ts

Lines changed: 137 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -482,13 +482,13 @@ test('parseScript: push of a push statement', t => {
482482
});
483483

484484
test('parseScript: push of an evaluation', t => {
485-
t.deepEqual(parseScript('<$(<1> <2> OP_ADD)> OP_EQUAL'), {
485+
t.deepEqual(parseScript('<$(<1><2> OP_ADD)> OP_EQUAL'), {
486486
status: true,
487487
value: {
488488
end: {
489-
column: 29,
489+
column: 28,
490490
line: 1,
491-
offset: 28
491+
offset: 27
492492
},
493493
name: 'Script',
494494
start: {
@@ -499,9 +499,9 @@ test('parseScript: push of an evaluation', t => {
499499
value: [
500500
{
501501
end: {
502-
column: 20,
502+
column: 19,
503503
line: 1,
504-
offset: 19
504+
offset: 18
505505
},
506506
name: 'Push',
507507
start: {
@@ -511,9 +511,9 @@ test('parseScript: push of an evaluation', t => {
511511
},
512512
value: {
513513
end: {
514-
column: 19,
514+
column: 18,
515515
line: 1,
516-
offset: 18
516+
offset: 17
517517
},
518518
name: 'Script',
519519
start: {
@@ -524,9 +524,9 @@ test('parseScript: push of an evaluation', t => {
524524
value: [
525525
{
526526
end: {
527-
column: 19,
527+
column: 18,
528528
line: 1,
529-
offset: 18
529+
offset: 17
530530
},
531531
name: 'Evaluation',
532532
start: {
@@ -536,9 +536,9 @@ test('parseScript: push of an evaluation', t => {
536536
},
537537
value: {
538538
end: {
539-
column: 18,
539+
column: 17,
540540
line: 1,
541-
offset: 17
541+
offset: 16
542542
},
543543
name: 'Script',
544544
start: {
@@ -591,40 +591,40 @@ test('parseScript: push of an evaluation', t => {
591591
},
592592
{
593593
end: {
594-
column: 11,
594+
column: 10,
595595
line: 1,
596-
offset: 10
596+
offset: 9
597597
},
598598
name: 'Push',
599599
start: {
600-
column: 8,
600+
column: 7,
601601
line: 1,
602-
offset: 7
602+
offset: 6
603603
},
604604
value: {
605605
end: {
606-
column: 10,
606+
column: 9,
607607
line: 1,
608-
offset: 9
608+
offset: 8
609609
},
610610
name: 'Script',
611611
start: {
612-
column: 9,
612+
column: 8,
613613
line: 1,
614-
offset: 8
614+
offset: 7
615615
},
616616
value: [
617617
{
618618
end: {
619-
column: 10,
619+
column: 9,
620620
line: 1,
621-
offset: 9
621+
offset: 8
622622
},
623623
name: 'BigIntLiteral',
624624
start: {
625-
column: 9,
625+
column: 8,
626626
line: 1,
627-
offset: 8
627+
offset: 7
628628
},
629629
value: BigInt(2)
630630
}
@@ -633,15 +633,15 @@ test('parseScript: push of an evaluation', t => {
633633
},
634634
{
635635
end: {
636-
column: 18,
636+
column: 17,
637637
line: 1,
638-
offset: 17
638+
offset: 16
639639
},
640640
name: 'Identifier',
641641
start: {
642-
column: 12,
642+
column: 11,
643643
line: 1,
644-
offset: 11
644+
offset: 10
645645
},
646646
value: 'OP_ADD'
647647
}
@@ -653,15 +653,15 @@ test('parseScript: push of an evaluation', t => {
653653
},
654654
{
655655
end: {
656-
column: 29,
656+
column: 28,
657657
line: 1,
658-
offset: 28
658+
offset: 27
659659
},
660660
name: 'Identifier',
661661
start: {
662-
column: 21,
662+
column: 20,
663663
line: 1,
664-
offset: 20
664+
offset: 19
665665
},
666666
value: 'OP_EQUAL'
667667
}
@@ -672,11 +672,11 @@ test('parseScript: push of an evaluation', t => {
672672

673673
test('parseScript: invalid identifier characters', t => {
674674
t.deepEqual(parseScript('test_$variable'), {
675-
expected: ['EOF', 'whitespace'],
675+
expected: ["the opening parenthesis of this evaluation ('(')"],
676676
index: {
677-
column: 6,
677+
column: 7,
678678
line: 1,
679-
offset: 5
679+
offset: 6
680680
},
681681
status: false
682682
});
@@ -703,33 +703,118 @@ test('parseScript: incomplete UTF8Literal', t => {
703703
});
704704
});
705705

706-
test('parseScript: incomplete BigIntLiteral', t => {
707-
t.deepEqual(parseScript('1234a 42 "abc"'), {
708-
expected: ['EOF', 'whitespace'],
709-
index: {
710-
column: 5,
711-
line: 1,
712-
offset: 4
713-
},
714-
status: false
706+
test('parseScript: BigIntLiteral without trailing whitespace', t => {
707+
t.deepEqual(parseScript('1234a'), {
708+
status: true,
709+
value: {
710+
end: {
711+
column: 6,
712+
line: 1,
713+
offset: 5
714+
},
715+
name: 'Script',
716+
start: {
717+
column: 1,
718+
line: 1,
719+
offset: 0
720+
},
721+
value: [
722+
{
723+
end: {
724+
column: 5,
725+
line: 1,
726+
offset: 4
727+
},
728+
name: 'BigIntLiteral',
729+
start: {
730+
column: 1,
731+
line: 1,
732+
offset: 0
733+
},
734+
value: BigInt(1234)
735+
},
736+
{
737+
end: {
738+
column: 6,
739+
line: 1,
740+
offset: 5
741+
},
742+
name: 'Identifier',
743+
start: {
744+
column: 5,
745+
line: 1,
746+
offset: 4
747+
},
748+
value: 'a'
749+
}
750+
]
751+
}
715752
});
716753
});
717754

718-
test('parseScript: incomplete HexLiteral', t => {
755+
test('parseScript: HexLiteral without trailing whitespace', t => {
719756
t.deepEqual(parseScript('0x010203f'), {
720-
expected: ['EOF', 'whitespace'],
721-
index: {
722-
column: 9,
723-
line: 1,
724-
offset: 8
725-
},
726-
status: false
757+
status: true,
758+
value: {
759+
end: {
760+
column: 10,
761+
line: 1,
762+
offset: 9
763+
},
764+
name: 'Script',
765+
start: {
766+
column: 1,
767+
line: 1,
768+
offset: 0
769+
},
770+
value: [
771+
{
772+
end: {
773+
column: 9,
774+
line: 1,
775+
offset: 8
776+
},
777+
name: 'HexLiteral',
778+
start: {
779+
column: 1,
780+
line: 1,
781+
offset: 0
782+
},
783+
value: '010203'
784+
},
785+
{
786+
end: {
787+
column: 10,
788+
line: 1,
789+
offset: 9
790+
},
791+
name: 'Identifier',
792+
start: {
793+
column: 9,
794+
line: 1,
795+
offset: 8
796+
},
797+
value: 'f'
798+
}
799+
]
800+
}
727801
});
728802
});
729803

730804
test('parseScript: incomplete push', t => {
731805
t.deepEqual(parseScript('<my_var'), {
732-
expected: ["the end of this push statement ('>')", 'whitespace'],
806+
expected: [
807+
'a double quote (")',
808+
"a hex literal ('0x...')",
809+
"a single quote (')",
810+
'a valid identifier',
811+
'an integer literal',
812+
"the end of this push statement ('>')",
813+
"the start of a multi-line comment ('/*')",
814+
"the start of a push statement ('<')",
815+
"the start of a single-line comment ('//')",
816+
"the start of an evaluation ('$')"
817+
],
733818
index: {
734819
column: 8,
735820
line: 1,

src/lib/auth/templates/language/parse.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const authenticationScriptParser = P.createLanguage({
99
script: r =>
1010
P.seqMap(
1111
P.optWhitespace,
12-
r.expression.sepBy(P.whitespace).node('Script'),
12+
r.expression.sepBy(P.optWhitespace).node('Script'),
1313
P.optWhitespace,
1414
(_, expressions) => expressions
1515
),

0 commit comments

Comments
 (0)