Class: Yarr::InputParser

Inherits:
Parslet::Parser
  • Object
show all
Defined in:
lib/yarr/input_parser.rb

Overview

Note:

We also accept % character in names to support like queries.

User input parser

Recognised commands: ast{lang}>>, tok{lang}>>, asm{lang}>>, bare{lang}>>, {lang}>>, asm, ri, list, fake, renick, fact

The lang part is optional and it stands for a 2 digit ruby version specifier, like 21 for 2.1. The rest of the input is context specific, and parsed based on the command.

Evaluate commands

The command is followed by an arbitrary ruby expression which is subsequently sent to the evaluator. The AST always has a key :evaluate which can be used to identify the command type. If ast>>, tok>>, asm>>is used the sub part of AST will contain a :mode set to 'ast', 'tok' or 'asm'. It can also contain a :lang which is set to the optional ruby version. The ast always contains a top level :code that contains the code to evaluate.

p = Yarr::InputParser.new
p.parse('>> 1+1')
# => #<Yarr::AST @hash={:evaluate=>"", :code=>" 1+1"}>
p.parse('ast>> 1+1')
# => #<Yarr::AST @hash={:evaluate=>{:mode=>"ast"}, :code=>" 1+1"}>
p.parse('ast12>> 1+1')
# => #<Yarr::AST @hash={:evaluate=>{:mode=>"ast", :lang=>"12"},
#                                   :code=>" 1+1"}>

Non-evaluate commands

The user input contains 3 sections, 2 required and 1 optional. The sections are separated by white spaces. Between the second and third section an optional comma is allowed. A usual user input might look like:

list Array phaul
ri Set, phaul
  • The first word list is the command.

  • The second part is until the next `,', whitespace, or til the end: Array. This part is the command target.

  • The third part is optional: phaul. The code refers to this part as “stuff” and it's copied straight to the output, or in case if it's a nick, we prefix our response with it.

The target portion of the user input is ri notation ruby token. (Normal Ruby token except # is used for instance methods.) We can parse:

  • method names

  • class names

  • instance method calls

  • class method calls

Examples

p = Yarr::InputParser.new
p.parse 'ri a' # => {:command=>"ri", :method_name=>"a"}
p.parse 'list B' # => {:command=>"list", :class_name=>"B"}
pp p.parse 'ri A#b, phaul'
# >> {:command=>"ri",
# >>  :instance_method=>{:class_name=>"A", :method_name=>"b"},
# >>  :stuff=>"phaul"}
pp p.parse 'ast A.b'
# >> {:command=>"ast",
# >>  :class_method=>{:class_name=>"A", :method_name=>"b"}}

No argument commands

Simple word commands

- renick

Defined Under Namespace

Classes: ParseError

Constant Summary collapse

OPERATORS =

Ruby operators % is not in the list because then we would match the first % in %x% as an operator, and then fail to parse the rest.

%w[[]= === <=>
!~ != [] >> >= =~ == <= << ** [email protected] [email protected]
! ^ > < / - + * & ~ `].freeze

Instance Method Summary collapse

Instance Method Details

#parse(string, *args) ⇒ AST

Same as Parslet::Parser#parse, except we return string hash values


189
190
191
192
193
# File 'lib/yarr/input_parser.rb', line 189

def parse(string, *args)
  AST.new(super)
rescue Parslet::ParseFailed => e
  raise ParseError, e
end