ns-macro-processor 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/ns_macro_processor.rb +83 -0
- data/lib/{processor_error.rb → ns_macro_processor/processor_error.rb} +1 -1
- data/lib/{syntax_error.rb → ns_macro_processor/syntax_error.rb} +1 -1
- data/lib/ns_macro_processor/tokens.rb +63 -0
- data/ns-macro-processor.gemspec +1 -1
- metadata +5 -5
- data/lib/macro_processor.rb +0 -79
- data/lib/tokens.rb +0 -59
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d4c6acbce70533c0ec4bb0401502331b160d627c
|
4
|
+
data.tar.gz: 36221f37910e0e2071ab17cb0d9d137e3f8e224b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 563814813617135dd792b371f2bba02b0fef481336d03ae3ca69d838fa7ec96a0f7439ef85393cef55a32a15b3a21719e07b16dc2ebc6dd4c000d6f8d6d6ba17
|
7
|
+
data.tar.gz: 4aa822f7c63e5cd17bdbf73b27d286ebea08678f7be79b1046cc2e5f5430ac53a5385575ff7e8da99365fcf14b0f54e17c1c0f34e61ae17a5c1b235ed7d0f3f8
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require_relative 'ns_macro_processor/processor_error'
|
2
|
+
require_relative 'ns_macro_processor/syntax_error'
|
3
|
+
require_relative 'ns_macro_processor/tokens'
|
4
|
+
|
5
|
+
module NsMacroProcessor
|
6
|
+
|
7
|
+
class Processor
|
8
|
+
|
9
|
+
def initialize(params)
|
10
|
+
@params = params
|
11
|
+
end
|
12
|
+
|
13
|
+
def parse(text)
|
14
|
+
@tokens = Tokens.new(text)
|
15
|
+
@tokens.advance
|
16
|
+
result = words
|
17
|
+
expect(Tokens::EOF)
|
18
|
+
result
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def words
|
24
|
+
result = []
|
25
|
+
while @tokens.peek != Tokens::EOF
|
26
|
+
result << word
|
27
|
+
end
|
28
|
+
result.join
|
29
|
+
end
|
30
|
+
|
31
|
+
def word
|
32
|
+
result = @tokens.peek
|
33
|
+
@tokens.advance
|
34
|
+
if result == '~' and accept('{')
|
35
|
+
@tokens.push_back(macro)
|
36
|
+
result = word
|
37
|
+
end
|
38
|
+
result
|
39
|
+
end
|
40
|
+
|
41
|
+
def macro
|
42
|
+
args = []
|
43
|
+
loop do
|
44
|
+
args << word
|
45
|
+
break if @tokens.peek == '}' || @tokens.peek == Tokens::EOF
|
46
|
+
end
|
47
|
+
expect('}')
|
48
|
+
execute(args.join.split(' '))
|
49
|
+
end
|
50
|
+
|
51
|
+
def execute(args)
|
52
|
+
if args[0] == 'include'
|
53
|
+
return lookup(:filestore).contents_of(args[1])
|
54
|
+
else
|
55
|
+
return lookup(args[0])
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def lookup(field_name)
|
60
|
+
value = @params[field_name] || @params[field_name.to_sym]
|
61
|
+
raise ProcessorError.new(field_name) unless value
|
62
|
+
value
|
63
|
+
end
|
64
|
+
|
65
|
+
def accept(s)
|
66
|
+
if (@tokens.peek == s)
|
67
|
+
@tokens.advance
|
68
|
+
true
|
69
|
+
else
|
70
|
+
false
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def expect(s)
|
75
|
+
return true if accept(s)
|
76
|
+
raise SyntaxError.new(s, @tokens.peek)
|
77
|
+
false
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module NsMacroProcessor
|
2
|
+
|
3
|
+
class Tokens
|
4
|
+
|
5
|
+
EOF = -1
|
6
|
+
|
7
|
+
def initialize(input)
|
8
|
+
@input = input
|
9
|
+
end
|
10
|
+
|
11
|
+
def advance
|
12
|
+
if @input.nil? || @input.empty?
|
13
|
+
@current = EOF
|
14
|
+
return
|
15
|
+
end
|
16
|
+
ix = 0
|
17
|
+
if whitespace?(@input[ix])
|
18
|
+
while ix < @input.length && whitespace?(@input[ix])
|
19
|
+
ix += 1
|
20
|
+
end
|
21
|
+
@current = ' '
|
22
|
+
@input = @input[ix..-1]
|
23
|
+
return
|
24
|
+
end
|
25
|
+
if ident_start?(@input[ix])
|
26
|
+
while ix < @input.length && ident?(@input[ix])
|
27
|
+
ix += 1
|
28
|
+
end
|
29
|
+
@current = @input[0, ix]
|
30
|
+
@input = @input[ix..-1]
|
31
|
+
return
|
32
|
+
end
|
33
|
+
@current = @input[0]
|
34
|
+
@input = @input[1..-1]
|
35
|
+
end
|
36
|
+
|
37
|
+
def peek
|
38
|
+
@current
|
39
|
+
end
|
40
|
+
|
41
|
+
def push_back(str)
|
42
|
+
@input = (@current == EOF) ? str + @input : str + @current + @input
|
43
|
+
advance
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def ident_start?(ch)
|
49
|
+
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_/".include?(ch)
|
50
|
+
end
|
51
|
+
|
52
|
+
def ident?(ch)
|
53
|
+
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-/_".include?(ch)
|
54
|
+
end
|
55
|
+
|
56
|
+
def whitespace?(ch)
|
57
|
+
" \t\n\r".include?(ch)
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
|
data/ns-macro-processor.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ns-macro-processor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kevin Rutherford
|
@@ -35,10 +35,10 @@ files:
|
|
35
35
|
- Gemfile
|
36
36
|
- Gemfile.lock
|
37
37
|
- Makefile
|
38
|
-
- lib/
|
39
|
-
- lib/processor_error.rb
|
40
|
-
- lib/syntax_error.rb
|
41
|
-
- lib/tokens.rb
|
38
|
+
- lib/ns_macro_processor.rb
|
39
|
+
- lib/ns_macro_processor/processor_error.rb
|
40
|
+
- lib/ns_macro_processor/syntax_error.rb
|
41
|
+
- lib/ns_macro_processor/tokens.rb
|
42
42
|
- ns-macro-processor.gemspec
|
43
43
|
homepage: https://github.com/kevinrutherford/ns-macro-processor
|
44
44
|
licenses:
|
data/lib/macro_processor.rb
DELETED
@@ -1,79 +0,0 @@
|
|
1
|
-
require_relative './processor_error'
|
2
|
-
require_relative './syntax_error'
|
3
|
-
require_relative './tokens'
|
4
|
-
|
5
|
-
class MacroProcessor
|
6
|
-
|
7
|
-
def initialize(params)
|
8
|
-
@params = params
|
9
|
-
end
|
10
|
-
|
11
|
-
def parse(text)
|
12
|
-
@tokens = Tokens.new(text)
|
13
|
-
@tokens.advance
|
14
|
-
result = words
|
15
|
-
expect(Tokens::EOF)
|
16
|
-
result
|
17
|
-
end
|
18
|
-
|
19
|
-
private
|
20
|
-
|
21
|
-
def words
|
22
|
-
result = []
|
23
|
-
while @tokens.peek != Tokens::EOF
|
24
|
-
result << word
|
25
|
-
end
|
26
|
-
result.join
|
27
|
-
end
|
28
|
-
|
29
|
-
def word
|
30
|
-
result = @tokens.peek
|
31
|
-
@tokens.advance
|
32
|
-
if result == '~' and accept('{')
|
33
|
-
@tokens.push_back(macro)
|
34
|
-
result = word
|
35
|
-
end
|
36
|
-
result
|
37
|
-
end
|
38
|
-
|
39
|
-
def macro
|
40
|
-
args = []
|
41
|
-
loop do
|
42
|
-
args << word
|
43
|
-
break if @tokens.peek == '}' || @tokens.peek == Tokens::EOF
|
44
|
-
end
|
45
|
-
expect('}')
|
46
|
-
execute(args.join.split(' '))
|
47
|
-
end
|
48
|
-
|
49
|
-
def execute(args)
|
50
|
-
if args[0] == 'include'
|
51
|
-
return lookup(:filestore).contents_of(args[1])
|
52
|
-
else
|
53
|
-
return lookup(args[0])
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
def lookup(field_name)
|
58
|
-
value = @params[field_name] || @params[field_name.to_sym]
|
59
|
-
raise ProcessorError.new(field_name) unless value
|
60
|
-
value
|
61
|
-
end
|
62
|
-
|
63
|
-
def accept(s)
|
64
|
-
if (@tokens.peek == s)
|
65
|
-
@tokens.advance
|
66
|
-
true
|
67
|
-
else
|
68
|
-
false
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
def expect(s)
|
73
|
-
return true if accept(s)
|
74
|
-
raise SyntaxError.new(s, @tokens.peek)
|
75
|
-
false
|
76
|
-
end
|
77
|
-
|
78
|
-
end
|
79
|
-
|
data/lib/tokens.rb
DELETED
@@ -1,59 +0,0 @@
|
|
1
|
-
class Tokens
|
2
|
-
|
3
|
-
EOF = -1
|
4
|
-
|
5
|
-
def initialize(input)
|
6
|
-
@input = input
|
7
|
-
end
|
8
|
-
|
9
|
-
def advance
|
10
|
-
if @input.nil? || @input.empty?
|
11
|
-
@current = EOF
|
12
|
-
return
|
13
|
-
end
|
14
|
-
ix = 0
|
15
|
-
if whitespace?(@input[ix])
|
16
|
-
while ix < @input.length && whitespace?(@input[ix])
|
17
|
-
ix += 1
|
18
|
-
end
|
19
|
-
@current = ' '
|
20
|
-
@input = @input[ix..-1]
|
21
|
-
return
|
22
|
-
end
|
23
|
-
if ident_start?(@input[ix])
|
24
|
-
while ix < @input.length && ident?(@input[ix])
|
25
|
-
ix += 1
|
26
|
-
end
|
27
|
-
@current = @input[0, ix]
|
28
|
-
@input = @input[ix..-1]
|
29
|
-
return
|
30
|
-
end
|
31
|
-
@current = @input[0]
|
32
|
-
@input = @input[1..-1]
|
33
|
-
end
|
34
|
-
|
35
|
-
def peek
|
36
|
-
@current
|
37
|
-
end
|
38
|
-
|
39
|
-
def push_back(str)
|
40
|
-
@input = (@current == EOF) ? str + @input : str + @current + @input
|
41
|
-
advance
|
42
|
-
end
|
43
|
-
|
44
|
-
private
|
45
|
-
|
46
|
-
def ident_start?(ch)
|
47
|
-
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_/".include?(ch)
|
48
|
-
end
|
49
|
-
|
50
|
-
def ident?(ch)
|
51
|
-
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-/_".include?(ch)
|
52
|
-
end
|
53
|
-
|
54
|
-
def whitespace?(ch)
|
55
|
-
" \t\n\r".include?(ch)
|
56
|
-
end
|
57
|
-
|
58
|
-
end
|
59
|
-
|