Module: KPeg

Defined in:
lib/kpeg.rb,
lib/kpeg/match.rb,
lib/kpeg/parser.rb,
lib/kpeg/grammar.rb,
lib/kpeg/position.rb,
lib/kpeg/code_generator.rb,
lib/kpeg/compiled_parser.rb,
lib/kpeg/grammar_renderer.rb

Defined Under Namespace

Modules: Position Classes: Action, AndPredicate, Bounds, CharRange, Choice, CodeGenerator, Collect, CompiledParser, Dot, ForeignInvokeRule, FormatParser, Grammar, GrammarRenderer, InvokeRule, LiteralRegexp, LiteralString, Match, MatchComposition, MatchString, Multiple, NotPredicate, Operator, Parser, Rule, RuleReference, Sequence, StringEscape, Tag

Constant Summary collapse

VERSION =
"1.1.0"

Class Method Summary collapse

Class Method Details

.compile(str, name, scope = Object) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/kpeg.rb', line 37

def self.compile(str, name, scope=Object)
  parser = KPeg::FormatParser.new str
  unless parser.parse
    parser.raise_error
  end

  cg = KPeg::CodeGenerator.new name, parser.grammar

  code = cg.output

  scope.module_eval code
  true
end

.grammar {|g| ... } ⇒ Object

Yields:

  • (g)


5
6
7
8
9
# File 'lib/kpeg.rb', line 5

def self.grammar
  g = Grammar.new
  yield g
  g
end

.load(file, name) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/kpeg.rb', line 25

def self.load(file, name)
  grammar = load_grammar(file)
  cg = KPeg::CodeGenerator.new name, grammar

  code = cg.output

  warn "[Loading parser '#{name}' => #{code.size} bytes]"

  Object.module_eval code
  true
end

.load_grammar(file, log = false) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/kpeg.rb', line 16

def self.load_grammar(file, log=false)
  parser = KPeg::FormatParser.new File.read(file)
  if !parser.parse
    parser.raise_error
  end

  return parser.grammar
end

.match(str, gram) ⇒ Object



11
12
13
14
# File 'lib/kpeg.rb', line 11

def self.match(str, gram)
  scan = Parser.new(str, gram)
  scan.parse
end