1 # coding: utf-8
2 # frozen_string_literal: true
3
4
5
6 module Umu
7
8 module ConcreteSyntax
9
10 module Core
11
12 module Expression
13
14 module Unary
15
16 module Identifier
17
18 class Abstract < Unary::Abstract; end
19
20
21
22 class Short < Abstract
23 alias sym obj
24
25
26 def initialize(loc, sym)
27 ASSERT.kind_of sym, ::Symbol
28
29 super
30 end
31
32
33 def to_s
34 self.sym.to_s
35 end
36
37
38 private
39
40 def __desugar__(_env, _event)
41 ASCE.make_identifier self.loc, self.sym
42 end
43 end
44
45
46
47 class Long < Abstract
48 alias head_id obj
49 attr_reader :tail_ids
50
51
52 def initialize(loc, head_id, tail_ids)
53 ASSERT.kind_of head_id, Short
54 ASSERT.kind_of tail_ids, ::Array
55
56 super(loc, head_id)
57
58 @tail_ids = tail_ids
59 end
60
61
62 def to_s
63 if tail_ids.empty?
64 self.head_id.to_s
65 else
66 format("%s::%s",
67 self.head_id.to_s,
68 self.tail_ids.map(&:to_s).join('::')
69 )
70 end
71 end
72
73
74 private
75
76 def __desugar__(env, event)
77 new_env = env.enter event
78
79 if tail_ids.empty?
80 self.head_id.desugar(new_env)
81 else
82 ASCE.make_long_identifier(
83 loc,
84 self.head_id.desugar(new_env),
85 self.tail_ids.map { |id| id.desugar(new_env) }
86 )
87 end
88 end
89 end
90
91 end # Umu::ConcreteSyntax::Core::Expression::Unary::Identifier
92
93 end # Umu::ConcreteSyntax::Core::Expression::Unary
94
95
96 module_function
97
98 def make_identifier(loc, sym)
99 ASSERT.kind_of loc, LOC::Entry
100 ASSERT.kind_of sym, ::Symbol
101
102 Unary::Identifier::Short.new(loc, sym).freeze
103 end
104
105
106 def make_long_identifier(loc, head_id, tail_ids)
107 ASSERT.kind_of head_id, Unary::Identifier::Short
108 ASSERT.kind_of tail_ids, ::Array
109
110 Unary::Identifier::Long.new(loc, head_id, tail_ids.freeze).freeze
111 end
112
113 end # Umu::ConcreteSyntax::Core::Expression
114
115 end # Umu::ConcreteSyntax::Core
116
117 end # Umu::ConcreteSyntax
118
119 end # Umu