Debian Codegraph @

core/miku/parser.rb

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 # hatsune lisp is moest language
3
4 require_relative 'miku'
5 require_relative 'error'
6 require 'stringio'
7
8 module MIKU
9
10 def self.parse(str)
11 if(str.is_a?(String))
12 _parse(StringIO.new(str, 'r').extend(StaticCode))
13 else
14 str.extend(StaticCode) if not str.is_a? StaticCode
15 str.staticcode_file = str.path if defined? str.path
16 _parse(str)
17 end
18 end
19
20 def self._parse(s)
21 while(true) do
22 c = skipspace(s)
23 if c == ';'
24 _comment(s)
25 else
26 break end end
27 pos = s.staticcode_dump
28 r = case c
29 when '(' then
30 _list(s)
31 when '#' then
32 _structure(s)
33 when '"' then
34 _string(s)
35 when '`' then
36 Cons.list(:backquote, _parse(s)).extend(StaticCode).staticcode_copy_info(pos)
37 when ',' then
38 c = s.getc.chr
39 if c == '@' then
40 Cons.list(:comma_at, _parse(s)).extend(StaticCode).staticcode_copy_info(pos)
41 else
42 s.ungetc(c[0])
43 Cons.list(:comma, _parse(s)).extend(StaticCode).staticcode_copy_info(pos)
44 end
45 when '\'' then
46 Cons.list(:quote, _parse(s)).extend(StaticCode).staticcode_copy_info(pos)
47 else
48 _symbol(c, s)
49 end
50 _after(s, r)
51 end
52
53 def self._after(s, r)
54 read_to(s){ |c| not(c =~ /[\t ]/) }
55 skipped = s.getc
56 if skipped.respond_to?(:chr) and skipped.chr == "["
57 pos = s.staticcode_dump
58 return _after(s, Cons.new(r, _list(s, ']').extend(StaticCode).staticcode_copy_info(pos)
59 ).extend(StaticCode).staticcode_copy_info(pos))
60 else
61 s.ungetc(skipped) if skipped
62 end
63 r
64 end
65
66 def self._comment(s)
67 read_to(s){ |c| c == "\n" }
68 end
69
70 def self._structure(s)
71 c = skipspace(s)
72 pos = s.staticcode_dump
73 if(c == '(')
74 Cons.new(:lambda, _list(s)).extend(StaticCode).staticcode_copy_info(pos)
75 else
76 type = _symbol(c, s)
77 c = skipspace(s)
78 if c != '('
79 raise SyntaxError.new("##{type}の後に文字#{c}があります。必ず中括弧をおいてください",s)
80 end
81 pos = s.staticcode_dump
82 lst = _list(s).extend(StaticCode).staticcode_copy_info(pos)
83 case
84 when [:array, :a].include?(type)
85 lst.to_a.extend(StaticCode).staticcode_copy_info(pos)
86 when [:hash, :h].include?(type)
87 genlist = Cons.new(:list, lst).extend(StaticCode).staticcode_copy_info(pos)
88 Cons.list(:to_hash, genlist).extend(StaticCode).staticcode_copy_info(pos)
89 # Hash[*lst.to_a].extend(StaticCode).staticcode_copy_info(pos)
90 when [:lambda, :function, :func, :f].include?(type)
91 Cons.new(:lambda, lst).extend(StaticCode).staticcode_copy_info(pos) end end end
92
93 def self._list(s, pend=')')
94 scd = s.staticcode_dump
95 c = s.getc.chr
96 return nil if c == pend
97 s.ungetc(c[0])
98 car = _parse(s)
99 c = skipspace(s)
100 if(c == '.') then
101 cdr = _parse(s)
102 s.ungetc(skipspace(s)[0])
103 raise SyntaxError.new('ドット対がちゃんと終わってないよ',s) if(s.getc.chr != pend)
104 return Cons.new(car, cdr).extend(StaticCode).staticcode_copy_info(scd)
105 else
106 s.ungetc(c[0])
107 return Cons.new(car, _list(s, pend)).extend(StaticCode).staticcode_copy_info(scd)
108 end
109 end
110
111 def self._string(s)
112 result = read_to(s){ |c| c == '"' }
113 s.getc
114 result.extend(StaticCode).staticcode_copy_info(s)
115 end
116
117 def self._symbol(c, s)
118 sym = c + read_to(s){ |c| not(c =~ /[^\(\)\{}\[\].',#\s]/) }
119 raise SyntaxError.new('### 深刻なエラーが発生しました ###',s) if not(sym)
120 if(sym =~ /^-?[0-9]+$/) then
121 sym.to_i
122 elsif(sym =~ /^-?[0-9]+\.[0-9]+$/) then
123 sym.to_f
124 elsif(sym == 'nil') then
125 nil
126 elsif(sym == '')
127 raise MIKU::EndofFile
128 else
129 sym.to_sym
130 end
131 end
132
133 def self.read_to(s, escape=false, &cond)
134 c = s.getc
135 return '' if not c
136 c = c.chr
137 if !escape and cond.call(c)
138 s.ungetc(c[0])
139 return '' end
140 case
141 when '\\' == c
142 read_to(s, true, &cond)
143 when ('n' == c) && escape
144 "\n" + read_to(s, &cond)
145 else
146 c + read_to(s, &cond) end
147 end
148
149 def self.skipspace(s)
150 c = s.getc
151 return '' if not c
152 c = c.chr
153 s.staticcode_line += 1 if c == "\n"
154 return skipspace(s) if(c =~ /\s/)
155 c
156 end
157
158 def self.unparse(val)
159 if val === nil
160 'nil'
161 elsif(val.is_a?(List))
162 val.unparse
163 elsif(val.is_a?(String))
164 '"' + val.dup.gsub("\n", '\\n').gsub('"', '\\"') + '"'
165 elsif(val.respond_to?(:unparse))
166 val.unparse
167 else
168 val.inspect end end
169
170 end