Created
          November 24, 2010 12:44 
        
      - 
      
 - 
        
Save manveru/713591 to your computer and use it in GitHub Desktop.  
    Minimal treetop example
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | require 'treetop' | |
| grammar = Treetop.load_from_string(DATA.read) | |
| parser = MyGrammarParser.new | |
| p parser.parse(" a = 1 \n b = 2 \n").value | |
| __END__ | |
| grammar MyGrammar | |
| rule main | |
| pair* { | |
| def value | |
| elements.map(&:key_value) | |
| end | |
| } | |
| end | |
| rule pair | |
| space keyword:(keyword) space '=' space value:(value) space eol { | |
| def key_value | |
| [keyword.text_value, value.text_value] | |
| end | |
| } | |
| end | |
| rule space | |
| ' '+ | |
| end | |
| rule keyword | |
| [a-z]+ | |
| end | |
| rule value | |
| [0-9]+ | |
| end | |
| rule eol | |
| "\n"+ | |
| end | |
| end | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment