Class: Resyma::Language
- Inherits:
-
Object
- Object
- Resyma::Language
- Defined in:
- lib/resyma/language.rb
Direct Known Subclasses
LangDate, LangRubymoji, LangTOML, LangTOMLNamespace, LangTimeline
Instance Attribute Summary collapse
-
#engine ⇒ Object
readonly
Returns the value of attribute engine.
Class Method Summary collapse
-
.load(&block) ⇒ Object
Initialize a new instance without argument and call ‘#load`.
Instance Method Summary collapse
- #build_language(procedure) ⇒ Object
- #built? ⇒ Boolean
-
#initialize ⇒ Language
constructor
A new instance of Language.
-
#load(&block) ⇒ Object
Load a block as DSL.
-
#load_ast(ast, binding, filename, lineno) ⇒ Object
Interpret the AST as current language.
-
#load_parsetree!(parsetree, binding, filename, lineno, need_clean = true) ⇒ Object
Interpret the parse tree as current language.
- #syntax ⇒ Object
Constructor Details
#initialize ⇒ Language
Returns a new instance of Language.
193 194 195 196 197 198 |
# File 'lib/resyma/language.rb', line 193 def initialize # @type [Array<Resyma::MonoLanguage>] @mono_languages = nil # @type [Resyma::Core::Engine] @engine = nil end |
Instance Attribute Details
#engine ⇒ Object (readonly)
Returns the value of attribute engine.
200 201 202 |
# File 'lib/resyma/language.rb', line 200 def engine @engine end |
Class Method Details
.load(&block) ⇒ Object
Initialize a new instance without argument and call ‘#load`
286 287 288 |
# File 'lib/resyma/language.rb', line 286 def self.load(&block) new.load(&block) end |
Instance Method Details
#build_language(procedure) ⇒ Object
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/resyma/language.rb', line 204 def build_language(procedure) ast, bd, filename, lino = Resyma.source_of(procedure) body_ast = Resyma.extract_body(ast) if body_ast.nil? raise LanguageSyntaxError, "Define your language by override method syntax" end if body_ast.type == :begin @mono_languages = body_ast.children.map do |stm_ast| MonoLanguage.from(stm_ast, bd, filename, lino) end @engine = Resyma::Core::Engine.new(@mono_languages.map(&:automaton)) else @mono_languages = [MonoLanguage.from(body_ast, bd, filename, lino)] @engine = Resyma::Core::Engine.new(@mono_languages.first.automaton) end end |
#built? ⇒ Boolean
222 223 224 |
# File 'lib/resyma/language.rb', line 222 def built? @mono_languages && @engine end |
#load(&block) ⇒ Object
Load a block as DSL. Note that argument of the block will be ignored.
273 274 275 276 277 278 |
# File 'lib/resyma/language.rb', line 273 def load(&block) ast, bd, filename, = Resyma.source_of(block) body_ast = Resyma.extract_body(ast) lino = body_ast.loc.expression.line load_ast(body_ast, bd, filename, lino) end |
#load_ast(ast, binding, filename, lineno) ⇒ Object
Interpret the AST as current language
263 264 265 266 |
# File 'lib/resyma/language.rb', line 263 def load_ast(ast, binding, filename, lineno) parsetree = Core::DEFAULT_CONVERTER.convert(ast) load_parsetree!(parsetree, binding, filename, lineno, false) end |
#load_parsetree!(parsetree, binding, filename, lineno, need_clean = true) ⇒ Object
Interpret the parse tree as current language. Note that the original evaluation result in the tree will be overlapped.
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/resyma/language.rb', line 237 def load_parsetree!(parsetree, binding, filename, lineno, need_clean = true) build_language(method(:syntax)) unless built? parsetree.clear! if need_clean parsetree.parent = nil @engine.traverse!(parsetree) accepted_set = @engine.accepted_tuples(parsetree) tuple4 = accepted_set.min_by(&:belongs_to) if tuple4.nil? raise LanguageSyntaxError, "The code does not adhere to syntax defined by #{self.class}" end dns = @engine.backtrack_for(parsetree, tuple4) nodes = dns.map { |t| @engine.node_of(parsetree, t.p_) } action = @mono_languages[tuple4.belongs_to].action ae = ActionEnvironment.new(nodes, binding, filename, lineno) action.call(ae) end |
#syntax ⇒ Object
202 |
# File 'lib/resyma/language.rb', line 202 def syntax; end |