Skip to content

FarmDrop/indentation-parser

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Indentation-Parser

Build Status Code Climate

Parses source code that defines context by indentation.

How

Assume you have this structure:

this
  is
    an
      example : First example
    the
      second
        one : Second example
  serves
    as
      another
        example : Third example

You want to create an output which you can use like this:

puts output.this.is.an.example
# => "First example"

puts output.this.is.the.second.one
# => "Second example"

puts output.this.serves.as.another.example
# => "Third example"

You write a parser like so:

parser = IndentationParser.new do |p|
  p.default do |parent, source|
    node = OpenStruct.new
    parent.send("#{source}=", node)
    node
  end
  p.on /([^ ]+) : (.+)/ do |parent, source, captures|
    node = captures[2]
    parent.send("#{captures[1]}=", node)
    node
  end
end

Then you read your special syntax from a file and parse it:

source = IO.read("path/to/file")
output = parser.read(source, OpenStruct.new).value

puts output.this.is.an.example
puts output.this.is.the.second.one
puts output.this.serves.as.another.example

Hooks

The following hooks are available:

p.on /regex/ do |parent, source, captures|
  #...
end
p.default do |parent, source|
  #...
end
p.on_leaf /regex/ do |parent, source, captures|
  #...
end
p.on_leaf do |parent, source|
  #...
end
p.as_a_child_of Type do |parent, source|
  #...
end

About

Parses source code that defines context by indentation.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Ruby 100.0%