Skip to content

Commit

Permalink
Allow snip arguments to contain spaces even when not quoted.
Browse files Browse the repository at this point in the history
  • Loading branch information
lazyatom committed Aug 27, 2009
1 parent ea4f5e9 commit 15d0e4d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 45 deletions.
91 changes: 52 additions & 39 deletions lib/vanilla/snip_reference.rb
Expand Up @@ -27,7 +27,7 @@ def _nt_snip_call
end

i0, s0 = index, []
if input.index("{", index) == index
if has_terminal?("{", false, index)
r1 = instantiate_node(SyntaxNode,input, index...(index + 1))
@index += 1
else
Expand All @@ -50,7 +50,7 @@ def _nt_snip_call
r4 = instantiate_node(SyntaxNode,input, i4...index, s4)
r4.extend(SnipCall0)
else
self.index = i4
@index = i4
r4 = nil
end
if r4
Expand All @@ -60,7 +60,7 @@ def _nt_snip_call
end
s0 << r3
if r3
if input.index("}", index) == index
if has_terminal?("}", false, index)
r7 = instantiate_node(SyntaxNode,input, index...(index + 1))
@index += 1
else
Expand All @@ -75,13 +75,13 @@ def _nt_snip_call
r0 = instantiate_node(SnipCall,input, i0...index, s0)
r0.extend(SnipCall1)
else
self.index = i0
@index = i0
r0 = nil
end

node_cache[:snip_call][start_index] = r0

return r0
r0
end

module SnipName0
Expand All @@ -107,7 +107,7 @@ def _nt_snip_name
r2 = _nt_word
s1 << r2
if r2
if input.index(".", index) == index
if has_terminal?(".", false, index)
r3 = instantiate_node(SyntaxNode,input, index...(index + 1))
@index += 1
else
Expand All @@ -124,7 +124,7 @@ def _nt_snip_name
r1 = instantiate_node(SnipNameWithAttribute,input, i1...index, s1)
r1.extend(SnipName0)
else
self.index = i1
@index = i1
r1 = nil
end
if r1
Expand All @@ -135,14 +135,14 @@ def _nt_snip_name
if r5
r0 = r5
else
self.index = i0
@index = i0
r0 = nil
end
end

node_cache[:snip_name][start_index] = r0

return r0
r0
end

module ArgumentList0
Expand Down Expand Up @@ -185,7 +185,7 @@ def _nt_argument_list
r3 = instantiate_node(SyntaxNode,input, i3...index, s3)
r3.extend(ArgumentList0)
else
self.index = i3
@index = i3
r3 = nil
end
if r3
Expand All @@ -199,13 +199,13 @@ def _nt_argument_list
r0 = instantiate_node(ArgumentList,input, i0...index, s0)
r0.extend(ArgumentList1)
else
self.index = i0
@index = i0
r0 = nil
end

node_cache[:argument_list][start_index] = r0

return r0
r0
end

def _nt_argument
Expand All @@ -216,12 +216,25 @@ def _nt_argument
return cached
end

r0 = _nt_word
r0.extend(NormalArgument)
i0 = index
r1 = _nt_unquoted_words
r1.extend(NormalArgument)
if r1
r0 = r1
else
r2 = _nt_quoted_word
r2.extend(NormalArgument)
if r2
r0 = r2
else
@index = i0
r0 = nil
end
end

node_cache[:argument][start_index] = r0

return r0
r0
end

module ArgumentSeparator0
Expand All @@ -239,7 +252,7 @@ def _nt_argument_separator
end

i0, s0 = index, []
if input.index(",", index) == index
if has_terminal?(",", false, index)
r1 = instantiate_node(SyntaxNode,input, index...(index + 1))
@index += 1
else
Expand All @@ -255,13 +268,13 @@ def _nt_argument_separator
r0 = instantiate_node(SyntaxNode,input, i0...index, s0)
r0.extend(ArgumentSeparator0)
else
self.index = i0
@index = i0
r0 = nil
end

node_cache[:argument_separator][start_index] = r0

return r0
r0
end

def _nt_word
Expand All @@ -281,14 +294,14 @@ def _nt_word
if r2
r0 = r2
else
self.index = i0
@index = i0
r0 = nil
end
end

node_cache[:word][start_index] = r0

return r0
r0
end

def _nt_unquoted_word
Expand All @@ -301,7 +314,7 @@ def _nt_unquoted_word

s0, i0 = [], index
loop do
if input.index(Regexp.new('[a-zA-Z0-9_\\-]'), index) == index
if has_terminal?('\G[a-zA-Z0-9_\\-]', true, index)
r1 = instantiate_node(SyntaxNode,input, index...(index + 1))
@index += 1
else
Expand All @@ -314,15 +327,15 @@ def _nt_unquoted_word
end
end
if s0.empty?
self.index = i0
@index = i0
r0 = nil
else
r0 = instantiate_node(SyntaxNode,input, i0...index, s0)
end

node_cache[:unquoted_word][start_index] = r0

return r0
r0
end

module QuotedWord0
Expand All @@ -341,7 +354,7 @@ def _nt_quoted_word

i0 = index
i1, s1 = index, []
if input.index("\"", index) == index
if has_terminal?("\"", false, index)
r2 = instantiate_node(SyntaxNode,input, index...(index + 1))
@index += 1
else
Expand All @@ -353,7 +366,7 @@ def _nt_quoted_word
r3 = _nt_unquoted_words
s1 << r3
if r3
if input.index("\"", index) == index
if has_terminal?("\"", false, index)
r4 = instantiate_node(SyntaxNode,input, index...(index + 1))
@index += 1
else
Expand All @@ -367,14 +380,14 @@ def _nt_quoted_word
r1 = instantiate_node(QuotedWord,input, i1...index, s1)
r1.extend(QuotedWord0)
else
self.index = i1
@index = i1
r1 = nil
end
if r1
r0 = r1
else
i5, s5 = index, []
if input.index("'", index) == index
if has_terminal?("'", false, index)
r6 = instantiate_node(SyntaxNode,input, index...(index + 1))
@index += 1
else
Expand All @@ -386,7 +399,7 @@ def _nt_quoted_word
r7 = _nt_unquoted_words
s5 << r7
if r7
if input.index("'", index) == index
if has_terminal?("'", false, index)
r8 = instantiate_node(SyntaxNode,input, index...(index + 1))
@index += 1
else
Expand All @@ -400,20 +413,20 @@ def _nt_quoted_word
r5 = instantiate_node(QuotedWord,input, i5...index, s5)
r5.extend(QuotedWord1)
else
self.index = i5
@index = i5
r5 = nil
end
if r5
r0 = r5
else
self.index = i0
@index = i0
r0 = nil
end
end

node_cache[:quoted_word][start_index] = r0

return r0
r0
end

def _nt_unquoted_words
Expand All @@ -426,7 +439,7 @@ def _nt_unquoted_words

s0, i0 = [], index
loop do
if input.index(Regexp.new('[a-zA-Z0-9_\\- ]'), index) == index
if has_terminal?('\G[a-zA-Z0-9_\\- ]', true, index)
r1 = instantiate_node(SyntaxNode,input, index...(index + 1))
@index += 1
else
Expand All @@ -439,15 +452,15 @@ def _nt_unquoted_words
end
end
if s0.empty?
self.index = i0
@index = i0
r0 = nil
else
r0 = instantiate_node(SyntaxNode,input, i0...index, s0)
end

node_cache[:unquoted_words][start_index] = r0

return r0
r0
end

def _nt_optional_spaces
Expand All @@ -460,7 +473,7 @@ def _nt_optional_spaces

s0, i0 = [], index
loop do
if input.index(Regexp.new('[ ]'), index) == index
if has_terminal?('\G[ ]', true, index)
r1 = instantiate_node(SyntaxNode,input, index...(index + 1))
@index += 1
else
Expand All @@ -476,7 +489,7 @@ def _nt_optional_spaces

node_cache[:optional_spaces][start_index] = r0

return r0
r0
end

def _nt_spaces
Expand All @@ -489,7 +502,7 @@ def _nt_spaces

s0, i0 = [], index
loop do
if input.index(Regexp.new('[ ]'), index) == index
if has_terminal?('\G[ ]', true, index)
r1 = instantiate_node(SyntaxNode,input, index...(index + 1))
@index += 1
else
Expand All @@ -502,15 +515,15 @@ def _nt_spaces
end
end
if s0.empty?
self.index = i0
@index = i0
r0 = nil
else
r0 = instantiate_node(SyntaxNode,input, i0...index, s0)
end

node_cache[:spaces][start_index] = r0

return r0
r0
end

end
Expand Down
3 changes: 2 additions & 1 deletion lib/vanilla/snip_reference.treetop
Expand Up @@ -13,7 +13,8 @@ grammar SnipReference
end

rule argument
word <NormalArgument>
unquoted_words <NormalArgument> /
quoted_word <NormalArgument>
end

rule argument_separator
Expand Down
13 changes: 8 additions & 5 deletions lib/vanilla/snip_reference_parser.rb
Expand Up @@ -57,7 +57,7 @@ def to_arguments

if __FILE__ == $0

Treetop.load "snip_reference"
Treetop.load "vanilla/snip_reference"
require 'test/unit'

class SnipReferenceParserTest < Test::Unit::TestCase
Expand All @@ -79,6 +79,8 @@ class SnipReferenceParserTest < Test::Unit::TestCase
%|{snip."spaced attribute"}| => {:snip => 'snip', :attribute => 'spaced attribute', :arguments => []},
%|{"snip with spaces".attribute}| => {:snip => 'snip with spaces', :attribute => 'attribute', :arguments => []},
%|{snip.snip_attribute arg}| => {:snip => 'snip', :attribute => 'snip_attribute', :arguments => ['arg']},
%|{snip arg with spaces}| => {:snip => 'snip', :attribute => nil, :arguments => ['arg with spaces']},
%|{snip arg with spaces, another arg}| => {:snip => 'snip', :attribute => nil, :arguments => ['arg with spaces', 'another arg']},
# %|{snip key1:value1,key2:value2}| => {:snip => 'snip', :arguments => {:key1 => 'value1', :key2 => 'value2'}},
# %|{snip key1:value1, key2:value2}| => {:snip => 'snip', :arguments => {:key}},
# %|{snip key1: value1, key2: value2}|,
Expand All @@ -94,10 +96,11 @@ def setup

examples.each do |example, expected|
define_method :"test_parsing_#{example}" do
tree = @parser.parse(example)
if tree
assert_equal expected[:snip], tree.snip
assert_equal expected[:arguments], tree.arguments
reference = @parser.parse(example)
if reference
assert_equal expected[:snip], reference.snip
assert_equal expected[:attribute], reference.attribute
assert_equal expected[:arguments], reference.arguments
else
flunk "failed to parse: #{example}"
end
Expand Down

0 comments on commit 15d0e4d

Please sign in to comment.