@@ -5,14 +5,12 @@ module ERB
5
5
class Parser
6
6
# This is the parent class of any kind of errors that will be raised by
7
7
# the parser.
8
- class ParseError < StandardError
9
- end
10
8
11
9
# This error occurs when a certain token is expected in a certain place
12
10
# but is not found. Sometimes this is handled internally because some
13
11
# elements are optional. Other times it is not and it is raised to end the
14
12
# parsing process.
15
- class MissingTokenError < ParseError
13
+ class MissingTokenError < SyntaxTree :: Parser :: ParseError
16
14
end
17
15
18
16
attr_reader :source , :tokens
@@ -52,7 +50,13 @@ def parse_any_tag
52
50
53
51
if tag . is_a? ( Doctype )
54
52
if @found_doctype
55
- raise ( ParseError , "Only one doctype element is allowed" )
53
+ raise (
54
+ SyntaxTree ::Parser ::ParseError . new (
55
+ "Only one doctype element is allowed" ,
56
+ tag . location . start_line ,
57
+ 0
58
+ )
59
+ )
56
60
else
57
61
@found_doctype = true
58
62
end
@@ -123,8 +127,13 @@ def make_tokens
123
127
# abc
124
128
enum . yield :text , $&, index , line
125
129
else
126
- raise ParseError ,
127
- "Unexpected character at #{ index } : #{ source [ index ] } "
130
+ raise (
131
+ SyntaxTree ::Parser ::ParseError . new (
132
+ "Unexpected character at #{ index } : #{ source [ index ] } " ,
133
+ line ,
134
+ 0
135
+ )
136
+ )
128
137
end
129
138
in :erb_start
130
139
case source [ index ..]
@@ -207,8 +216,13 @@ def make_tokens
207
216
# abc
208
217
enum . yield :text , $&, index , line
209
218
else
210
- raise ParseError ,
211
- "Unexpected character in string at #{ index } : #{ source [ index ] } "
219
+ raise (
220
+ SyntaxTree ::Parser ::ParseError . new (
221
+ "Unexpected character in string at #{ index } : #{ source [ index ] } " ,
222
+ line ,
223
+ 0
224
+ )
225
+ )
212
226
end
213
227
in :string_double_quote
214
228
case source [ index ..]
@@ -230,8 +244,13 @@ def make_tokens
230
244
# abc
231
245
enum . yield :text , $&, index , line
232
246
else
233
- raise ParseError ,
234
- "Unexpected character in string at #{ index } : #{ source [ index ] } "
247
+ raise (
248
+ SyntaxTree ::Parser ::ParseError . new (
249
+ "Unexpected character in string at #{ index } : #{ source [ index ] } " ,
250
+ line ,
251
+ 0
252
+ )
253
+ )
235
254
end
236
255
in :inside
237
256
case source [ index ..]
@@ -284,8 +303,13 @@ def make_tokens
284
303
enum . yield :string_open_single_quote , $&, index , line
285
304
state << :string_single_quote
286
305
else
287
- raise ParseError ,
288
- "Unexpected character at #{ index } : #{ source [ index ] } "
306
+ raise (
307
+ SyntaxTree ::Parser ::ParseError . new (
308
+ "Unexpected character at #{ index } : #{ source [ index ] } " ,
309
+ line ,
310
+ 0
311
+ )
312
+ )
289
313
end
290
314
end
291
315
@@ -304,7 +328,13 @@ def consume(expected)
304
328
type , value , index , line = tokens . peek
305
329
306
330
if expected != type
307
- raise MissingTokenError , "expected #{ expected } got #{ type } "
331
+ raise (
332
+ MissingTokenError . new (
333
+ "expected #{ expected } got #{ type } " ,
334
+ line ,
335
+ index
336
+ )
337
+ )
308
338
end
309
339
310
340
tokens . next
@@ -335,7 +365,9 @@ def maybe
335
365
# Otherwise we'll return the value returned by the block.
336
366
def atleast
337
367
result = yield
338
- raise MissingTokenError if result . nil?
368
+ if result . nil?
369
+ raise ( MissingTokenError . new ( "No matching token" , nil , nil ) )
370
+ end
339
371
result
340
372
end
341
373
@@ -372,7 +404,13 @@ def parse_html_opening_tag
372
404
name = consume ( :name )
373
405
374
406
if name . value =~ /\A [@:#]/
375
- raise ParseError , "Invalid html-tag name #{ name } "
407
+ raise (
408
+ SyntaxTree ::Parser ::ParseError . new (
409
+ "Invalid html-tag name #{ name } " ,
410
+ name . location . start_line ,
411
+ 0
412
+ )
413
+ )
376
414
end
377
415
378
416
attributes =
@@ -431,15 +469,21 @@ def parse_html_element
431
469
432
470
if closing . nil?
433
471
raise (
434
- ParseError ,
435
- "Missing closing tag for <#{ opening . name . value } > at #{ opening . location } "
472
+ SyntaxTree ::Parser ::ParseError . new (
473
+ "Missing closing tag for <#{ opening . name . value } > at #{ opening . location } " ,
474
+ opening . location . start_line ,
475
+ 0
476
+ )
436
477
)
437
478
end
438
479
439
480
if closing . name . value != opening . name . value
440
481
raise (
441
- ParseError ,
442
- "Expected closing tag for <#{ opening . name . value } > but got <#{ closing . name . value } > at #{ closing . location } "
482
+ SyntaxTree ::Parser ::ParseError . new (
483
+ "Expected closing tag for <#{ opening . name . value } > but got <#{ closing . name . value } > at #{ closing . location } " ,
484
+ closing . location . start_line ,
485
+ 0
486
+ )
443
487
)
444
488
end
445
489
@@ -462,8 +506,11 @@ def parse_erb_case(erb_node)
462
506
unless erb_tag . is_a? ( ErbCaseWhen ) || erb_tag . is_a? ( ErbElse ) ||
463
507
erb_tag . is_a? ( ErbEnd )
464
508
raise (
465
- ParseError ,
466
- "Found no matching erb-tag to the if-tag at #{ erb_node . location } "
509
+ SyntaxTree ::Parser ::ParseError . new (
510
+ "Found no matching erb-tag to the if-tag at #{ erb_node . location } " ,
511
+ erb_node . location . start_line ,
512
+ 0
513
+ )
467
514
)
468
515
end
469
516
@@ -484,8 +531,11 @@ def parse_erb_case(erb_node)
484
531
)
485
532
else
486
533
raise (
487
- ParseError ,
488
- "Found no matching when- or else-tag to the case-tag at #{ erb_node . location } "
534
+ SyntaxTree ::Parser ::ParseError . new (
535
+ "Found no matching when- or else-tag to the case-tag at #{ erb_node . location } " ,
536
+ erb_node . location . start_line ,
537
+ 0
538
+ )
489
539
)
490
540
end
491
541
end
@@ -501,8 +551,11 @@ def parse_erb_if(erb_node)
501
551
502
552
unless erb_tag . is_a? ( ErbControl ) || erb_tag . is_a? ( ErbEnd )
503
553
raise (
504
- ParseError ,
505
- "Found no matching erb-tag to the if-tag at #{ erb_node . location } "
554
+ SyntaxTree ::Parser ::ParseError . new (
555
+ "Found no matching erb-tag to the if-tag at #{ erb_node . location } " ,
556
+ erb_node . location . start_line ,
557
+ 0
558
+ )
506
559
)
507
560
end
508
561
@@ -530,8 +583,11 @@ def parse_erb_if(erb_node)
530
583
)
531
584
else
532
585
raise (
533
- ParseError ,
534
- "Found no matching elsif- or else-tag to the if-tag at #{ erb_node . location } "
586
+ SyntaxTree ::Parser ::ParseError . new (
587
+ "Found no matching elsif- or else-tag to the if-tag at #{ erb_node . location } " ,
588
+ erb_node . location . start_line ,
589
+ 0
590
+ )
535
591
)
536
592
end
537
593
end
@@ -543,8 +599,11 @@ def parse_erb_else(erb_node)
543
599
544
600
unless erb_end . is_a? ( ErbEnd )
545
601
raise (
546
- ParseError ,
547
- "Found no matching end-tag for the else-tag at #{ erb_node . location } "
602
+ SyntaxTree ::Parser ::ParseError . new (
603
+ "Found no matching end-tag for the else-tag at #{ erb_node . location } " ,
604
+ erb_node . location . start_line ,
605
+ 0
606
+ )
548
607
)
549
608
end
550
609
@@ -582,8 +641,11 @@ def parse_erb_tag
582
641
583
642
if !closing_tag . is_a? ( ErbClose )
584
643
raise (
585
- ParseError ,
586
- "Found no matching closing tag for the erb-tag at #{ opening_tag . location } "
644
+ SyntaxTree ::Parser ::ParseError . new (
645
+ "Found no matching closing tag for the erb-tag at #{ opening_tag . location } " ,
646
+ opening_tag . location . start_line ,
647
+ 0
648
+ )
587
649
)
588
650
end
589
651
@@ -615,8 +677,11 @@ def parse_erb_tag
615
677
616
678
unless erb_end . is_a? ( ErbEnd )
617
679
raise (
618
- ParseError ,
619
- "Found no matching end-tag for the do-tag at #{ erb_node . location } "
680
+ SyntaxTree ::Parser ::ParseError . new (
681
+ "Found no matching end-tag for the do-tag at #{ erb_node . location } " ,
682
+ erb_node . location . start_line ,
683
+ 0
684
+ )
620
685
)
621
686
end
622
687
@@ -630,13 +695,23 @@ def parse_erb_tag
630
695
erb_node
631
696
end
632
697
end
633
- rescue MissingTokenError => error
698
+ rescue SyntaxTree :: Parser :: ParseError => error
634
699
# If we have parsed tokens that we cannot process after we parsed <%, we should throw a ParseError
635
700
# and not let it be handled by a `maybe`.
636
701
if opening_tag
702
+ message =
703
+ if error . message . include? ( "Could not parse ERB-tag" )
704
+ error . message
705
+ else
706
+ "Could not parse ERB-tag: #{ error . message } "
707
+ end
708
+
637
709
raise (
638
- ParseError ,
639
- "Could not parse ERB-tag at #{ opening_tag . location } "
710
+ SyntaxTree ::Parser ::ParseError . new (
711
+ message ,
712
+ opening_tag . location . start_line ,
713
+ 0
714
+ )
640
715
)
641
716
else
642
717
raise ( error )
0 commit comments