1 # coding: utf-8
2 # frozen_string_literal: true
3
4
5
6 module Umu
7
8 module Lexical
9
10 module Lexer
11
12 class Comment < Abstract
13 attr_reader :buf
14 attr_reader :saved_loc
15 attr_reader :comment_depth
16
17
18 def self.deconstruct_keys
19 {
20 :buf => ::String,
21 :saved_loc => LOC::Entry,
22 :comment_depth => ::Integer
23 }
24 end
25
26
27 def initialize(loc, braket_stack, buf, saved_loc, comment_depth)
28 ASSERT.kind_of loc, LOC::Entry
29 ASSERT.kind_of braket_stack, ::Array
30 ASSERT.kind_of buf, ::String
31 ASSERT.kind_of saved_loc, LOC::Entry
32 ASSERT.kind_of comment_depth, ::Integer
33
34 super(loc, braket_stack)
35
36 @buf = buf
37 @saved_loc = saved_loc
38 @comment_depth = comment_depth
39 end
40
41
42 def to_s
43 format("%s {braket_stack=%s, buf=%s, saved_loc=%s} -- %s",
44 E::Tracer.class_to_string(self.class),
45 self.braket_stack.inspect,
46 self.buf.inspect,
47 self.saved_loc.to_s,
48 self.loc.to_s
49 )
50 end
51
52
53 def in_comment?
54 self.comment_depth != 0
55 end
56
57
58 def lex(scanner)
59 ASSERT.kind_of scanner, ::StringScanner
60
61 case
62 # Begin-Comment
63 when scanner.scan(/\(#/)
64 [
65 :BeginComment,
66
67 scanner.matched,
68
69 [],
70
71 __make_comment__(
72 self.saved_loc,
73 self.comment_depth + 1,
74 self.buf + scanner.matched
75 )
76 ]
77
78 # End-Comment
79 when scanner.scan(/#\)/)
80 [
81 :EndComment,
82
83 scanner.matched,
84
85 [
86 LT.make_comment(
87 self.saved_loc, # Load Begin-Comment's location
88 self.buf
89 )
90 ],
91
92 if self.comment_depth <= 1
93 __make_separator__
94 else
95 __make_comment__(
96 self.saved_loc,
97 self.comment_depth - 1,
98 self.buf + scanner.matched
99 )
100 end
101 ]
102
103 # New-line
104 when scanner.skip(/\n/)
105 [
106 :NewLine,
107
108 scanner.matched,
109
110 [],
111
112 __make_comment__(
113 self.saved_loc,
114 self.comment_depth,
115 self.buf + scanner.matched,
116 self.loc.next_line_num
117 )
118 ]
119
120 # Others
121 when scanner.scan(/./)
122 [
123 :Other,
124
125 scanner.matched,
126
127 [],
128
129 __make_comment__(
130 self.saved_loc,
131 self.comment_depth,
132 self.buf + scanner.matched
133 )
134 ]
135
136 else
137 ASSERT.abort scanner.inspect
138 end
139 end
140 end
141
142 end # Umu::Lexical::Lexer
143
144 end # Umu::Lexical
145
146 end # Umu