Class: Yay::Parser

Inherits:
ParserGen show all
Defined in:
lib/yay/parser.rb

Constant Summary

Constants inherited from ParserGen

Yay::ParserGen::Racc_arg, Yay::ParserGen::Racc_debug_parser, Yay::ParserGen::Racc_token_to_s_table

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ParserGen

#_reduce_none

Constructor Details

#initialize(context_name = nil) ⇒ Parser

Returns a new instance of Parser.



17
18
19
20
# File 'lib/yay/parser.rb', line 17

def initialize context_name=nil
    @lexer = Yay::Lexer.new
	@lexer.context_name = context_name
end

Instance Attribute Details

#allow_defaultObject (readonly)

Returns the value of attribute allow_default.



12
13
14
# File 'lib/yay/parser.rb', line 12

def allow_default
  @allow_default
end

#allow_includeObject

Returns the value of attribute allow_include.



14
15
16
# File 'lib/yay/parser.rb', line 14

def allow_include
  @allow_include
end

#allow_installObject (readonly)

Returns the value of attribute allow_install.



13
14
15
# File 'lib/yay/parser.rb', line 13

def allow_install
  @allow_install
end

#allow_printObject (readonly)

Returns the value of attribute allow_print.



15
16
17
# File 'lib/yay/parser.rb', line 15

def allow_print
  @allow_print
end

Instance Method Details

#allow_all=(value) ⇒ Object

allow all parser actions



48
49
50
# File 'lib/yay/parser.rb', line 48

def allow_all= value
	@allow_default = @allow_install = @allow_include = @allow_print = value
end

#current_positionObject



146
147
148
# File 'lib/yay/parser.rb', line 146

def current_position
	return [@lexer.position, @lexer.line, @lexer.context_name]
end

#extract_regexp_options(args) ⇒ Object

for lack of a better function, this will take the ending sequence from a regular expression and convert it in to a bytewise options variable

Raises:

  • (ArgumentError)


72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/yay/parser.rb', line 72

def extract_regexp_options args
  return 0 if args.nil?
  raise ArgumentError unless args.kind_of? String
  options_map = {
    'i' => Regexp::IGNORECASE,
    'm' => Regexp::MULTILINE,
    'x' => Regexp::EXTENDED,
  }
  options = 0
  args.each { |char|
    options |= options_map[char] || 0
  }
  return options
end

#get_rulesObject



122
123
124
# File 'lib/yay/parser.rb', line 122

def get_rules
  @ruleset.get_rules
end

#handle_colours(colours) ⇒ Object

given an array of colour strings, create an array with the VT100 colour sequences inside



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/yay/parser.rb', line 100

def handle_colours colours
  fg = bg = nil
  result = []
  # iterate the colour list and try to find up to two colours (foreground,
  # background) and unlimited miscellaneous colours (reset, invert, etc)
  colours.each { |colour| 
    misc_val = ColourWheel::MISC[colour]
    if !misc_val.nil?
      result.push misc_val
    elsif !fg
      fg = ColourWheel::FG[colour]
      result.push fg
    elsif !bg
      bg = ColourWheel::BG[colour]
      result.push bg
    else
      raise Yay::TooManyColoursError.new fg, bg, colour, current_position
    end
  }
  result
end

#handle_regex(string) ⇒ Object



66
67
68
# File 'lib/yay/parser.rb', line 66

def handle_regex string
  return string_to_regex string, false
end

#handle_string(string) ⇒ Object



61
62
63
64
# File 'lib/yay/parser.rb', line 61

def handle_string string
  string = Regexp::escape(string)
  return Regexp.new(string, Regexp::IGNORECASE)
end

#include_file(filename) ⇒ Object

load a file from a url



27
28
29
30
31
32
# File 'lib/yay/parser.rb', line 27

def include_file filename
			raise NotAllowedError.new "include #{filename}", current_position unless @allow_include
  loader = Yay::Loader.new filename
  loader.load
  @ruleset.merge loader.get_rules
end

#install_file(url) ⇒ Object

install a file from a url



35
36
37
38
39
# File 'lib/yay/parser.rb', line 35

def install_file url
			raise NotAllowedError.new "install #{url}", current_position unless @allow_install
  installer = Yay::Installer.new url
  installer.install
end

#list_installedObject

print the full list of yay files



42
43
44
45
# File 'lib/yay/parser.rb', line 42

def list_installed
			raise NotAllowedError.new "list installed yay files", current_position unless @allow_print
  # TODO
end

#next_tokenObject

get the next token



142
143
144
# File 'lib/yay/parser.rb', line 142

def next_token
  @lexer.next_token
end

#on_error(error_token_id, error_value, cant_touch_this) ⇒ Object



150
151
152
153
# File 'lib/yay/parser.rb', line 150

def on_error error_token_id, error_value, cant_touch_this
  type = token_to_str error_token_id
  raise Yay::UnexpectedTokenError.new type, error_value, current_position
end

#parse(str) ⇒ Object

parse a string



133
134
135
136
137
138
139
# File 'lib/yay/parser.rb', line 133

def parse(str)
  @lexer.use_string(str)
  @ruleset = Yay::RuleSet.new

  do_parse
  get_rules
end

#parse_array(args) ⇒ Object

process commandline arguments as if they were from a yay file

Raises:

  • (ArgumentError)


127
128
129
130
# File 'lib/yay/parser.rb', line 127

def parse_array args
  raise ArgumentError, "args" unless args.kind_of? Array
  parse args.join(' ')
end

#string_to_regex(string, escape = true) ⇒ Object

for lack of a better function, this will take a string like “/abc/” and transform it in to a regex object



89
90
91
92
93
94
95
96
# File 'lib/yay/parser.rb', line 89

def string_to_regex string, escape=true
  matches = /\/([^\/\\\r\n]*(?:\\.[^\/\\\r\n]*)*)\/([a-z]\b)*/.match string
  return nil if matches[1].nil?
  content = matches[1]
	  content = Regexp::escape(content) if escape
  options = extract_regexp_options matches[2]
  return Regexp.new(content, options)
end

#use_default_fileObject

load the default file. used when the commandline is empty



53
54
55
56
57
58
59
# File 'lib/yay/parser.rb', line 53

def use_default_file
	# don't throw an error in this case. it's legitimate for a file to be empty
	return unless @allow_default
    loader = Yay::Loader.default_file_loader
    loader.load
    @ruleset.merge loader.get_rules
end