Class: Pione::Command::PioneLangInteractiveContext

Inherits:
Rootage::CommandContext show all
Defined in:
lib/pione/command/pione-lang-interactive.rb

Instance Attribute Summary

Attributes inherited from Rootage::ProcessContext

#model, #scenario

Instance Method Summary collapse

Methods inherited from Rootage::CommandContext

#quit, #stop

Methods inherited from Rootage::ProcessContext

#fail, #initialize, make, #test

Constructor Details

This class inherits a constructor from Rootage::ProcessContext

Instance Method Details

Print parsing result of the string.



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/pione/command/pione-lang-interactive.rb', line 119

def print_result(parser, str)
  begin
    stree = parser.parse(str)
    transformer_option = {}
    transformer_option[:package_name] = model[:package_name] || "PioneSyntaxChecker"
    transformer_option[:filename] = model[:filename] || "NoFile"
    model = Lang::DocumentTransformer.new.apply(stree, transformer_option)
    if model.kind_of?(Array)
      model.each {|m| p m}
    else
      p model.eval(Lang::Environment.new)
    end
  rescue Lang::ParserError, Parslet::ParseFailed => e
    msg = "Pione syntax error: %s (%s)" % [e.message, e.class.name]
    puts(msg)
  rescue Lang::LangTypeError, Lang::BindingError => e
    msg = "Pione model error: %s (%s)" % [e.message, e.class.name]
    puts(msg)
  rescue Lang::MethodNotFound => e
    msg = "%s (%s)" % [e.message, e.class.name]
    puts(msg)
  end
end

#start_readlineObject



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
# File 'lib/pione/command/pione-lang-interactive.rb', line 89

def start_readline
  buf = ""
  mark = ">"

  # start loop
  while line = Readline.readline("#{mark} ".color(:red), true)
    buf += line
    if /[^\s]/.match(buf)
      # don't record if previous line is the same
      if Readline::HISTORY.size > 1 && Readline::HISTORY[-2] == buf
        Readline::HISTORY.pop
      end
      if buf[-1] == "\\"
        buf[-1] = "\n"
        mark = "+"
        next
      else
        # print parsing result
        print_result(Lang::DocumentParser.new.expr, buf)
        buf = ""
        mark = ">"
      end
    else
      # don't record if it is an empty line
      Readline::HISTORY.pop
    end
  end
end