Skip to content
This repository has been archived by the owner on Dec 24, 2023. It is now read-only.

Commit

Permalink
Better structure
Browse files Browse the repository at this point in the history
  • Loading branch information
deathbeam committed Mar 21, 2016
1 parent 2960018 commit 30328b8
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions lib/spoon/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
require "spoon/util/indent_parser"

module Spoon
class Parser < Spoon::Util::IndentParser
# Monkey-patch the parser to include some common methods
class Spoon::Util::IndentParser
# Matches string and skips space after it
def sym(value) str(value) >> space? end

Expand Down Expand Up @@ -32,15 +33,20 @@ def parens(value) sym("(").maybe >> value >> sym(")").maybe end
# Matches everything until end of line
rule(:stop) { match["^\n"].repeat }

# Matches everything that starts with '#' until end of line
# example: # abc
rule(:comment) { str("#") >> stop.as(:comment) }
end

class Parser < Spoon::Util::IndentParser
# Matches entire file, skipping all whitespace at beginning and end
rule(:root) { whitespace? >> (expressions | statement.repeat(1)) >> whitespace? }

# Matches value
rule(:value) { condition | closure | name | number }

# Matches statement (unassignable and unmovable value)
rule(:statement) { function }
rule(:statements) { statement.repeat(1) }

# Matches entire file, skipping all whitespace at beginning and end
rule(:root) { whitespace? >> (expressions | statements) >> whitespace? }

# Matches indented block and consumes newlines at start and in between
# but not at end
Expand All @@ -54,10 +60,6 @@ def parens(value) sym("(").maybe >> value >> sym(")").maybe end
# Matches expression or indented block and skips end of line at end
rule(:body) { (block | expression) >> newline? }

# Matches everything that starts with '#' until end of line
# example: # abc
rule(:comment) { str("#") >> stop.as(:comment) }

# Matches all lowercase words except keys, then skips space after them
# example: abc
rule(:name) { skip_key >> match["a-z"].repeat(1).as(:name) >> space? }
Expand Down

0 comments on commit 30328b8

Please sign in to comment.