-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
parser.rb
977 lines (905 loc) · 33.9 KB
/
parser.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
# Copyright (C) 2011-2023 Apple Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
# THE POSSIBILITY OF SUCH DAMAGE.
require "config"
require "ast"
require "instructions"
require "pathname"
require "registers"
require "self_hash"
class SourceFile
@@fileNames = []
attr_reader :name, :basename, :fileNumber
def SourceFile.outputDotFileList(outp)
@@fileNames.each_index {
| index |
$asm.putStr "\".file #{index+1} \\\"#{@@fileNames[index]}\\\"\\n\""
}
end
def initialize(fileName)
@name = Pathname.new(fileName)
@basename = File.basename(fileName)
pathName = "#{@name.realpath}"
fileNumber = @@fileNames.index(pathName)
if not fileNumber
@@fileNames << pathName
fileNumber = @@fileNames.length
else
fileNumber += 1 # File numbers are 1 based
end
@fileNumber = fileNumber
end
end
class CodeOrigin
attr_reader :lineNumber
def initialize(sourceFile, lineNumber)
@sourceFile = sourceFile
@lineNumber = lineNumber
end
def fileName
@sourceFile.name
end
def debugDirective
"\".loc #{@sourceFile.fileNumber} #{lineNumber}\\n\""
end
def to_s
"#{@sourceFile.basename}:#{lineNumber}"
end
end
class IncludeFile
@@includeDirs = []
attr_reader :fileName
def initialize(moduleName, defaultDir)
directory = nil
@@includeDirs.each {
| includePath |
fileName = File.join(includePath, moduleName + ".asm")
directory = includePath unless not File.file?(fileName)
}
if not directory
directory = defaultDir
end
@fileName = File.join(directory, moduleName + ".asm")
end
def self.processIncludeOptions()
while ARGV[0][/^-I/]
path = ARGV.shift[2..-1]
if not path
path = ARGV.shift
end
@@includeDirs << (path + "/")
end
end
end
class Token
attr_reader :codeOrigin, :string
def initialize(codeOrigin, string)
@codeOrigin = codeOrigin
@string = string
end
def ==(other)
if other.is_a? Token
@string == other.string
else
@string == other
end
end
def =~(other)
@string =~ other
end
def to_s
"#{@string.inspect} at #{codeOrigin}"
end
def parseError(*comment)
if comment.empty?
raise "Parse error: #{to_s}"
else
raise "Parse error: #{to_s}: #{comment[0]}"
end
end
end
class Annotation
attr_reader :codeOrigin, :type, :string
def initialize(codeOrigin, type, string)
@codeOrigin = codeOrigin
@type = type
@string = string
end
end
#
# The lexer. Takes a string and returns an array of tokens.
#
def lex(str, file)
result = []
lineNumber = 1
annotation = nil
whitespaceFound = false
while not str.empty?
case str
when /\A\#([^\n]*)/
# ASM style single line comment, ignore
when /\A\/\*(\*(?!\/)|[^*])*\*\//
# C-style block comment, ignore
whitespaceFound = true
str = $~.post_match
comment = $&
done = comment.empty?
while not done
case comment
when /\n/
lineNumber += 1
comment = $~.post_match
else
done = true
end
end
next
when /\A\/\/\ ?([^\n]*)/
# annotation
annotation = $1
annotationType = whitespaceFound ? :local : :global
when /\A\n/
# We've found a '\n'. Emit the last comment recorded if appropriate:
# We need to parse annotations regardless of whether the backend does
# anything with them or not. This is because the C++ backend may make
# use of this for its cloopDo debugging utility even if
# enableInstrAnnotations is not enabled.
if annotation
result << Annotation.new(CodeOrigin.new(file, lineNumber),
annotationType, annotation)
annotation = nil
end
result << Token.new(CodeOrigin.new(file, lineNumber), $&)
lineNumber += 1
when /\A[a-zA-Z%]([a-zA-Z0-9_.%]*)/
result << Token.new(CodeOrigin.new(file, lineNumber), $&)
when /\A\.([a-zA-Z0-9_]*)/
result << Token.new(CodeOrigin.new(file, lineNumber), $&)
when /\A_([a-zA-Z0-9_%]*)/
result << Token.new(CodeOrigin.new(file, lineNumber), $&)
when /\A([ \t]+)/
# whitespace, ignore
whitespaceFound = true
str = $~.post_match
next
when /\A0x([0-9a-fA-F]+)/
result << Token.new(CodeOrigin.new(file, lineNumber), $&.hex.to_s)
when /\A0([0-7]+)/
result << Token.new(CodeOrigin.new(file, lineNumber), $&.oct.to_s)
when /\A([0-9]+)/
result << Token.new(CodeOrigin.new(file, lineNumber), $&)
when /\A::/
result << Token.new(CodeOrigin.new(file, lineNumber), $&)
when /\A[:,\(\)\[\]=\+\-~\|&^*]/
result << Token.new(CodeOrigin.new(file, lineNumber), $&)
when /\A".*"/
result << Token.new(CodeOrigin.new(file, lineNumber), $&)
when /\?/
result << Token.new(CodeOrigin.new(file, lineNumber), $&)
else
raise "Lexer error at #{CodeOrigin.new(file, lineNumber).to_s}, unexpected sequence #{str[0..20].inspect}"
end
whitespaceFound = false
str = $~.post_match
end
result
end
#
# Token identification.
#
def isRegister(token)
token =~ REGISTER_PATTERN
end
def isInstruction(token)
INSTRUCTION_SET.member? token.string
end
def isKeyword(token)
token =~ /\A((true)|(false)|(if)|(then)|(else)|(elsif)|(end)|(and)|(or)|(not)|(global)|(macro)|(const)|(constexpr)|(sizeof)|(error)|(include))\Z/ or
token =~ REGISTER_PATTERN or
isInstruction(token)
end
def isIdentifier(token)
token =~ /\A[a-zA-Z%]([a-zA-Z0-9_.%]*)\Z/ and not isKeyword(token)
end
def isLabel(token)
token =~ /\A_([a-zA-Z0-9_%]*)\Z/
end
def isLocalLabel(token)
token =~ /\A\.([a-zA-Z0-9_]*)\Z/
end
def isVariable(token)
isIdentifier(token) or isRegister(token)
end
def isInteger(token)
token =~ /\A[0-9]/
end
def isString(token)
token =~ /\A".*"/
end
#
# The parser. Takes an array of tokens and returns an AST. Methods
# other than parse(tokens) are not for public consumption.
#
class Parser
def initialize(data, fileName, options, sources=nil)
@tokens = lex(data, fileName)
@idx = 0
@annotation = nil
# FIXME: CMake does not currently set BUILT_PRODUCTS_DIR.
# https://bugs.webkit.org/show_bug.cgi?id=229340
@buildProductsDirectory = ENV['BUILT_PRODUCTS_DIR'];
@headersFolderPath = ENV['WK_LIBRARY_HEADERS_FOLDER_PATH'];
@options = options
@sources = sources
end
def parseError(*comment)
if @tokens[@idx]
@tokens[@idx].parseError(*comment)
else
if comment.empty?
raise "Parse error at end of file"
else
raise "Parse error at end of file: #{comment[0]}"
end
end
end
def consume(regexp)
if regexp
parseError unless @tokens[@idx] =~ regexp
else
parseError unless @idx == @tokens.length
end
@idx += 1
end
def skipNewLine
while @tokens[@idx] == "\n"
@idx += 1
end
end
def parsePredicateAtom
if @tokens[@idx] == "not"
codeOrigin = @tokens[@idx].codeOrigin
@idx += 1
Not.new(codeOrigin, parsePredicateAtom)
elsif @tokens[@idx] == "("
@idx += 1
skipNewLine
result = parsePredicate
parseError unless @tokens[@idx] == ")"
@idx += 1
result
elsif @tokens[@idx] == "true"
result = True.instance
@idx += 1
result
elsif @tokens[@idx] == "false"
result = False.instance
@idx += 1
result
elsif isIdentifier @tokens[@idx]
result = Setting.forName(@tokens[@idx].codeOrigin, @tokens[@idx].string)
@idx += 1
result
else
parseError
end
end
def parsePredicateAnd
result = parsePredicateAtom
while @tokens[@idx] == "and"
codeOrigin = @tokens[@idx].codeOrigin
@idx += 1
skipNewLine
right = parsePredicateAtom
result = And.new(codeOrigin, result, right)
end
result
end
def parsePredicate
# some examples of precedence:
# not a and b -> (not a) and b
# a and b or c -> (a and b) or c
# a or b and c -> a or (b and c)
result = parsePredicateAnd
while @tokens[@idx] == "or"
codeOrigin = @tokens[@idx].codeOrigin
@idx += 1
skipNewLine
right = parsePredicateAnd
result = Or.new(codeOrigin, result, right)
end
result
end
def parseVariable
if isRegister(@tokens[@idx])
if @tokens[@idx] =~ VEC_PATTERN
result = VecRegisterID.forName(@tokens[@idx].codeOrigin, @tokens[@idx].string)
elsif @tokens[@idx] =~ FPR_PATTERN || @tokens[@idx] =~ WASM_FPR_PATTERN
result = FPRegisterID.forName(@tokens[@idx].codeOrigin, @tokens[@idx].string)
else
result = RegisterID.forName(@tokens[@idx].codeOrigin, @tokens[@idx].string)
end
elsif isIdentifier(@tokens[@idx])
result = Variable.forName(@tokens[@idx].codeOrigin, @tokens[@idx].string)
else
parseError
end
@idx += 1
result
end
def parseConstExpr
if @tokens[@idx] == "constexpr"
@idx += 1
skipNewLine
if @tokens[@idx] == "("
codeOrigin, text = parseTextInParens
text = text.join
else
codeOrigin, text = parseColonColon
text = text.join("::")
end
ConstExpr.forName(codeOrigin, text)
else
parseError
end
end
def parseAddress(offset)
parseError unless @tokens[@idx] == "["
codeOrigin = @tokens[@idx].codeOrigin
# Three possibilities:
# [] -> AbsoluteAddress
# [a] -> Address
# [a,b] -> BaseIndex with scale = 1
# [a,b,c] -> BaseIndex
@idx += 1
if @tokens[@idx] == "]"
@idx += 1
return AbsoluteAddress.new(codeOrigin, offset)
end
a = parseVariable
if @tokens[@idx] == "]"
result = Address.new(codeOrigin, a, offset)
else
parseError unless @tokens[@idx] == ","
@idx += 1
b = parseVariable
if @tokens[@idx] == "]"
result = BaseIndex.new(codeOrigin, a, b, Immediate.new(codeOrigin, 1), offset)
else
parseError unless @tokens[@idx] == ","
@idx += 1
if ["1", "2", "4", "8"].member? @tokens[@idx].string
c = Immediate.new(codeOrigin, @tokens[@idx].string.to_i)
@idx += 1
elsif @tokens[@idx] == "constexpr"
c = parseConstExpr
else
c = parseVariable
end
parseError unless @tokens[@idx] == "]"
result = BaseIndex.new(codeOrigin, a, b, c, offset)
end
end
@idx += 1
result
end
def parseColonColon
skipNewLine
codeOrigin = @tokens[@idx].codeOrigin
parseError unless isIdentifier @tokens[@idx]
names = [@tokens[@idx].string]
@idx += 1
while @tokens[@idx] == "::"
@idx += 1
parseError unless isIdentifier @tokens[@idx]
names << @tokens[@idx].string
@idx += 1
end
raise if names.empty?
[codeOrigin, names]
end
def parseTextInParens
skipNewLine
codeOrigin = @tokens[@idx].codeOrigin
raise unless @tokens[@idx] == "("
@idx += 1
# need at least one item
raise if @tokens[@idx] == ")"
numEnclosedParens = 0
text = []
while @tokens[@idx] != ")" || numEnclosedParens > 0
if @tokens[@idx] == "("
numEnclosedParens += 1
elsif @tokens[@idx] == ")"
numEnclosedParens -= 1
end
text << @tokens[@idx].string
@idx += 1
end
@idx += 1
return [codeOrigin, text]
end
def parseExpressionAtom
skipNewLine
if @tokens[@idx] == "-"
@idx += 1
NegImmediate.new(@tokens[@idx - 1].codeOrigin, parseExpressionAtom)
elsif @tokens[@idx] == "~"
@idx += 1
BitnotImmediate.new(@tokens[@idx - 1].codeOrigin, parseExpressionAtom)
elsif @tokens[@idx] == "("
originalIndex = @idx
@idx += 1
result = parseExpression
parseError("expected ')' to match #{@tokens[originalIndex]}") unless @tokens[@idx] == ")"
@idx += 1
result
elsif isInteger @tokens[@idx]
result = Immediate.new(@tokens[@idx].codeOrigin, @tokens[@idx].string.to_i)
@idx += 1
result
elsif isString @tokens[@idx]
result = StringLiteral.new(@tokens[@idx].codeOrigin, @tokens[@idx].string)
@idx += 1
result
elsif isIdentifier @tokens[@idx]
codeOrigin, names = parseColonColon
if names.size > 1
StructOffset.forField(codeOrigin, names[0..-2].join('::'), names[-1])
else
Variable.forName(codeOrigin, names[0])
end
elsif isRegister @tokens[@idx]
parseVariable
elsif @tokens[@idx] == "sizeof"
@idx += 1
codeOrigin, names = parseColonColon
Sizeof.forName(codeOrigin, names.join('::'))
elsif @tokens[@idx] == "constexpr"
parseConstExpr
elsif isLabel @tokens[@idx]
result = LabelReference.new(@tokens[@idx].codeOrigin, Label.forName(@tokens[@idx].codeOrigin, @tokens[@idx].string))
@idx += 1
result
elsif isLocalLabel @tokens[@idx]
result = LocalLabelReference.new(@tokens[@idx].codeOrigin, LocalLabel.forName(@tokens[@idx].codeOrigin, @tokens[@idx].string))
@idx += 1
result
else
parseError
end
end
def parseExpressionMul
skipNewLine
result = parseExpressionAtom
while @tokens[@idx] == "*"
if @tokens[@idx] == "*"
@idx += 1
result = MulImmediates.new(@tokens[@idx - 1].codeOrigin, result, parseExpressionAtom)
else
raise
end
end
result
end
def couldBeExpression
@tokens[@idx] == "-" or @tokens[@idx] == "~" or @tokens[@idx] == "sizeof" or @tokens[@idx] == "constexpr" or isInteger(@tokens[@idx]) or isString(@tokens[@idx]) or isVariable(@tokens[@idx]) or isLabel(@tokens[@idx]) or @tokens[@idx] == "("
end
def parseExpressionAdd
skipNewLine
result = parseExpressionMul
while @tokens[@idx] == "+" or @tokens[@idx] == "-"
if @tokens[@idx] == "+"
@idx += 1
result = AddImmediates.new(@tokens[@idx - 1].codeOrigin, result, parseExpressionMul)
elsif @tokens[@idx] == "-"
@idx += 1
result = SubImmediates.new(@tokens[@idx - 1].codeOrigin, result, parseExpressionMul)
else
raise
end
end
result
end
def parseExpressionAnd
skipNewLine
result = parseExpressionAdd
while @tokens[@idx] == "&"
@idx += 1
result = AndImmediates.new(@tokens[@idx - 1].codeOrigin, result, parseExpressionAdd)
end
result
end
def parseExpression
skipNewLine
result = parseExpressionAnd
while @tokens[@idx] == "|" or @tokens[@idx] == "^"
if @tokens[@idx] == "|"
@idx += 1
result = OrImmediates.new(@tokens[@idx - 1].codeOrigin, result, parseExpressionAnd)
elsif @tokens[@idx] == "^"
@idx += 1
result = XorImmediates.new(@tokens[@idx - 1].codeOrigin, result, parseExpressionAnd)
else
raise
end
end
result
end
def parseOperand(comment)
skipNewLine
if couldBeExpression
expr = parseExpression
if @tokens[@idx] == "["
parseAddress(expr)
else
expr
end
elsif @tokens[@idx] == "["
parseAddress(Immediate.new(@tokens[@idx].codeOrigin, 0))
elsif isLocalLabel @tokens[@idx]
result = LocalLabelReference.new(@tokens[@idx].codeOrigin, LocalLabel.forName(@tokens[@idx].codeOrigin, @tokens[@idx].string))
@idx += 1
result
else
parseError(comment)
end
end
def parseMacroVariables
skipNewLine
consume(/\A\(\Z/)
variables = []
loop {
skipNewLine
if @tokens[@idx] == ")"
@idx += 1
break
elsif isIdentifier(@tokens[@idx])
variables << Variable.forName(@tokens[@idx].codeOrigin, @tokens[@idx].string)
@idx += 1
skipNewLine
if @tokens[@idx] == ")"
@idx += 1
break
elsif @tokens[@idx] == ","
@idx += 1
else
parseError
end
else
parseError
end
}
variables
end
def parseSequence(final, comment)
firstCodeOrigin = @tokens[@idx].codeOrigin
list = []
loop {
if @tokens[@idx].is_a? Annotation
# This is the only place where we can encounter a global
# annotation, and hence need to be able to distinguish between
# them.
# globalAnnotations are the ones that start from column 0. All
# others are considered localAnnotations. The only reason to
# distinguish between them is so that we can format the output
# nicely as one would expect.
codeOrigin = @tokens[@idx].codeOrigin
annotationOpcode = (@tokens[@idx].type == :global) ? "globalAnnotation" : "localAnnotation"
list << Instruction.new(codeOrigin, annotationOpcode, [], @tokens[@idx].string)
@annotation = nil
@idx += 2 # Consume the newline as well.
elsif (@idx == @tokens.length and not final) or (final and @tokens[@idx] =~ final)
break
elsif @tokens[@idx] == "\n"
# ignore
@idx += 1
elsif @tokens[@idx] == "const"
@idx += 1
parseError unless isVariable @tokens[@idx]
variable = Variable.forName(@tokens[@idx].codeOrigin, @tokens[@idx].string)
@idx += 1
parseError unless @tokens[@idx] == "="
@idx += 1
value = parseOperand("while inside of const #{variable.name}")
list << ConstDecl.new(@tokens[@idx].codeOrigin, variable, value)
elsif @tokens[@idx] == "error"
list << Error.new(@tokens[@idx].codeOrigin)
@idx += 1
elsif @tokens[@idx] == "if"
codeOrigin = @tokens[@idx].codeOrigin
@idx += 1
skipNewLine
predicate = parsePredicate
consume(/\A((then)|(\n))\Z/)
skipNewLine
ifThenElse = IfThenElse.new(codeOrigin, predicate, parseSequence(/\A((else)|(end)|(elsif))\Z/, "while inside of \"if #{predicate.dump}\""))
list << ifThenElse
while @tokens[@idx] == "elsif"
codeOrigin = @tokens[@idx].codeOrigin
@idx += 1
skipNewLine
predicate = parsePredicate
consume(/\A((then)|(\n))\Z/)
skipNewLine
elseCase = IfThenElse.new(codeOrigin, predicate, parseSequence(/\A((else)|(end)|(elsif))\Z/, "while inside of \"if #{predicate.dump}\""))
ifThenElse.elseCase = elseCase
ifThenElse = elseCase
end
if @tokens[@idx] == "else"
@idx += 1
ifThenElse.elseCase = parseSequence(/\Aend\Z/, "while inside of else case for \"if #{predicate.dump}\"")
@idx += 1
else
parseError unless @tokens[@idx] == "end"
@idx += 1
end
elsif @tokens[@idx] == "macro"
codeOrigin = @tokens[@idx].codeOrigin
@idx += 1
skipNewLine
parseError unless isIdentifier(@tokens[@idx])
name = @tokens[@idx].string
@idx += 1
variables = parseMacroVariables
body = parseSequence(/\Aend\Z/, "while inside of macro #{name}")
@idx += 1
list << Macro.new(codeOrigin, name, variables, body)
elsif @tokens[@idx] == "global"
codeOrigin = @tokens[@idx].codeOrigin
@idx += 1
skipNewLine
parseError unless isLabel(@tokens[@idx])
name = @tokens[@idx].string
@idx += 1
Label.setAsGlobal(codeOrigin, name)
elsif @tokens[@idx] == "globalexport"
codeOrigin = @tokens[@idx].codeOrigin
@idx += 1
skipNewLine
parseError unless isLabel(@tokens[@idx])
name = @tokens[@idx].string
@idx += 1
Label.setAsGlobalExport(codeOrigin, name)
elsif @tokens[@idx] == "unalignedglobal"
codeOrigin = @tokens[@idx].codeOrigin
@idx += 1
skipNewLine
parseError unless isLabel(@tokens[@idx])
name = @tokens[@idx].string
@idx += 1
Label.setAsUnalignedGlobal(codeOrigin, name)
elsif @tokens[@idx] == "aligned"
codeOrigin = @tokens[@idx].codeOrigin
@idx += 1
skipNewLine
parseError unless isLabel(@tokens[@idx])
name = @tokens[@idx].string
@idx += 1
align = @tokens[@idx].string
@idx += 1
Label.setAsAligned(codeOrigin, name, align)
elsif @tokens[@idx] == "unalignedglobalexport"
codeOrigin = @tokens[@idx].codeOrigin
@idx += 1
skipNewLine
parseError unless isLabel(@tokens[@idx])
name = @tokens[@idx].string
@idx += 1
Label.setAsUnalignedGlobalExport(codeOrigin, name)
elsif isInstruction @tokens[@idx]
codeOrigin = @tokens[@idx].codeOrigin
name = @tokens[@idx].string
@idx += 1
if (not final and @idx == @tokens.size) or (final and @tokens[@idx] =~ final)
# Zero operand instruction, and it's the last one.
list << Instruction.new(codeOrigin, name, [], @annotation)
@annotation = nil
break
elsif @tokens[@idx].is_a? Annotation
list << Instruction.new(codeOrigin, name, [], @tokens[@idx].string)
@annotation = nil
@idx += 2 # Consume the newline as well.
elsif @tokens[@idx] == "\n"
# Zero operand instruction.
list << Instruction.new(codeOrigin, name, [], @annotation)
@annotation = nil
@idx += 1
else
# It's definitely an instruction, and it has at least one operand.
operands = []
endOfSequence = false
loop {
operands << parseOperand("while inside of instruction #{name}")
if (not final and @idx == @tokens.size) or (final and @tokens[@idx] =~ final)
# The end of the instruction and of the sequence.
endOfSequence = true
break
elsif @tokens[@idx] == ","
# Has another operand.
@idx += 1
elsif @tokens[@idx].is_a? Annotation
@annotation = @tokens[@idx].string
@idx += 2 # Consume the newline as well.
break
elsif @tokens[@idx] == "\n"
# The end of the instruction.
@idx += 1
break
else
parseError("Expected a comma, newline, or #{final} after #{operands.last.dump}")
end
}
list << Instruction.new(codeOrigin, name, operands, @annotation)
@annotation = nil
if endOfSequence
break
end
end
# Check for potential macro invocation:
elsif isIdentifier @tokens[@idx]
codeOrigin = @tokens[@idx].codeOrigin
name = @tokens[@idx].string
@idx += 1
if @tokens[@idx] == "("
# Macro invocation.
@idx += 1
operands = []
skipNewLine
if @tokens[@idx] == ")"
@idx += 1
else
loop {
skipNewLine
if @tokens[@idx] == "macro"
# It's a macro lambda!
codeOriginInner = @tokens[@idx].codeOrigin
@idx += 1
variables = parseMacroVariables
body = parseSequence(/\Aend\Z/, "while inside of anonymous macro passed as argument to #{name}")
@idx += 1
operands << Macro.new(codeOriginInner, nil, variables, body)
else
operands << parseOperand("while inside of macro call to #{name}")
end
skipNewLine
if @tokens[@idx] == ")"
@idx += 1
break
elsif @tokens[@idx] == ","
@idx += 1
else
parseError "Unexpected #{@tokens[@idx].string.inspect} while parsing invocation of macro #{name}"
end
}
end
# Check if there's a trailing annotation after the macro invoke:
if @tokens[@idx].is_a? Annotation
@annotation = @tokens[@idx].string
@idx += 2 # Consume the newline as well.
end
list << MacroCall.new(codeOrigin, name, operands, @annotation)
@annotation = nil
else
parseError "Expected \"(\" after #{name}"
end
elsif isLabel @tokens[@idx] or isLocalLabel @tokens[@idx]
codeOrigin = @tokens[@idx].codeOrigin
name = @tokens[@idx].string
@idx += 1
parseError unless @tokens[@idx] == ":"
# It's a label.
if isLabel name
list << Label.forName(codeOrigin, name, true)
else
list << LocalLabel.forName(codeOrigin, name)
end
@idx += 1
elsif @tokens[@idx] == "include"
@idx += 1
isOptional = false
if @tokens[@idx] == "?"
isOptional = true
@idx += 1
end
parseError unless isIdentifier(@tokens[@idx])
moduleName = @tokens[@idx].string
@idx += 1
if @options[:webkit_additions_path]
additionsDirectoryName = @options[:webkit_additions_path]
else
additionsDirectoryName = "#{@buildProductsDirectory}#{@headersFolderPath}/WebKitAdditions/"
end
fileName = IncludeFile.new(moduleName, additionsDirectoryName).fileName
if not File.exist?(fileName)
fileName = IncludeFile.new(moduleName, @tokens[@idx].codeOrigin.fileName.dirname).fileName
end
fileExists = File.exist?(fileName)
raise "File not found: #{fileName}" if not fileExists and not isOptional
list << parse(fileName, @options, @sources) if fileExists
else
parseError "Expecting terminal #{final} #{comment}"
end
}
Sequence.new(firstCodeOrigin, list)
end
def parseIncludes(final, comment, options)
firstCodeOrigin = @tokens[@idx].codeOrigin
fileList = []
fileList << @tokens[@idx].codeOrigin.fileName
loop {
if (@idx == @tokens.length and not final) or (final and @tokens[@idx] =~ final)
break
elsif @tokens[@idx] == "include"
@idx += 1
isOptional = false
if @tokens[@idx] == "?"
isOptional = true
@idx += 1
end
parseError unless isIdentifier(@tokens[@idx])
moduleName = @tokens[@idx].string
@idx += 1
if @options[:webkit_additions_path]
additionsDirectoryName = @options[:webkit_additions_path]
else
additionsDirectoryName = "#{@buildProductsDirectory}#{@headersFolderPath}/WebKitAdditions/"
end
fileName = IncludeFile.new(moduleName, additionsDirectoryName).fileName
if not File.exist?(fileName)
fileName = IncludeFile.new(moduleName, @tokens[@idx].codeOrigin.fileName.dirname).fileName
end
fileExists = File.exist?(fileName)
raise "File not found: #{fileName}" if not fileExists and not isOptional
if fileExists
parser = Parser.new(readTextFile(fileName), SourceFile.new(fileName), options)
fileList << parser.parseIncludes(nil, "", options)
end
else
@idx += 1
end
}
return fileList
end
end
def readTextFile(fileName)
data = IO::read(fileName)
# On Windows, files may contain CRLF line endings (for example, git client might
# automatically replace \n with \r\n on Windows) which will fail our parsing.
# Thus, we'll just remove all \r from the data (keeping just the \n characters)
data.delete!("\r")
return data
end
def parseData(data, fileName, options, sources)
parser = Parser.new(data, SourceFile.new(fileName), options, sources)
parser.parseSequence(nil, "")
end
def parse(fileName, options, sources=nil)
sources << fileName if sources
parseData(readTextFile(fileName), fileName, options, sources)
end
def parseHash(fileName, options)
parser = Parser.new(readTextFile(fileName), SourceFile.new(fileName), options)
fileList = parser.parseIncludes(nil, "", options)
fileList.flatten!
fileListHash(fileList)
end