Class: ParserCombinator::StringParser

Inherits:
ParserCombinator show all
Defined in:
lib/parser_combinator/string_parser.rb

Constant Summary

Constants inherited from ParserCombinator

ParserCombinatorError, VERSION

Instance Attribute Summary

Attributes inherited from ParserCombinator

#parser_name, #parser_proc, #status_handler

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ParserCombinator

#<, #>, #>>, #^, binopl, binopl_fail, discardl, discardr, either, either_fail, end_of_input, fail, #initialize, item, many, many1, many1_fail, many_fail, #map, #name, ok, #onfail, #onparse, opt, opt_fail, #parse, parser, sat, seq, so_then, #|

Constructor Details

This class inherits a constructor from ParserCombinator

Class Method Details

.char(char) ⇒ Object



19
20
21
# File 'lib/parser_combinator/string_parser.rb', line 19

def self.char(char)
  sat{|c| c == char}
end

.convert_string_into_items(string, document_name) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/parser_combinator/string_parser.rb', line 5

def self.convert_string_into_items(string, document_name)
  integers = (1..100000).lazy
  items = string.each_line.zip(integers).map{|line, line_number|
    line.chars.zip(integers).map{|char, column_number|
      Item.new(char, :document_name => document_name, :line_number => line_number, :column_number => column_number)
    }
  }.flatten
  return Items.new(items)
end

.digitObject



41
42
43
# File 'lib/parser_combinator/string_parser.rb', line 41

def self.digit
  sat{|c| "0" <= c && c <= "9"}
end

.lower_alphaObject



33
34
35
# File 'lib/parser_combinator/string_parser.rb', line 33

def self.lower_alpha
  sat{|c| "a" <= c && c <= "z"}
end

.notchar(char) ⇒ Object



23
24
25
# File 'lib/parser_combinator/string_parser.rb', line 23

def self.notchar(char)
  sat{|c| c != char}
end

.pdigitObject



45
46
47
# File 'lib/parser_combinator/string_parser.rb', line 45

def self.pdigit
  sat{|c| "1" <= c && c <= "9"}
end

.str(object) ⇒ Object



27
28
29
30
31
# File 'lib/parser_combinator/string_parser.rb', line 27

def self.str(object)
  seq(*object.to_s.chars.map{|c| char(c)}) >> proc{|items|
    ok Items.new(items.to_a)
  }
end

.upper_alphaObject



37
38
39
# File 'lib/parser_combinator/string_parser.rb', line 37

def self.upper_alpha
  sat{|c| "A" <= c && c <= "Z"}
end

Instance Method Details

#parse_from_string(input_string, document_name = "anonymous") ⇒ Object



15
16
17
# File 'lib/parser_combinator/string_parser.rb', line 15

def parse_from_string(input_string, document_name="anonymous")
  parse(self.class.convert_string_into_items(input_string,  document_name))
end