ns-macro-processor 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bfda59e0f1b9049b21ab6414b488109a9d7dff24
4
+ data.tar.gz: cfea9027782ecaa67a87d4d97f2710a86791d50d
5
+ SHA512:
6
+ metadata.gz: f94af0efc77b90e3ed4a1b1675505f80f116fcaec23a0bd9e1984ce83575f23e8c0a9c07b6dfee504949aa335f4878bac804b30f2e2252b2c337da0a38e9f1ea
7
+ data.tar.gz: 685c9fcfdcc8dbc775513c851dc4e9e07ffbc271076eb06f10603ad0a64bc55c5f5163b0048c39a8842e729b03fbd7a1374644dc8fafa523eaf760d74295bc2d
@@ -0,0 +1,50 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ ## Specific to RubyMotion:
17
+ .dat*
18
+ .repl_history
19
+ build/
20
+ *.bridgesupport
21
+ build-iPhoneOS/
22
+ build-iPhoneSimulator/
23
+
24
+ ## Specific to RubyMotion (use of CocoaPods):
25
+ #
26
+ # We recommend against adding the Pods directory to your .gitignore. However
27
+ # you should judge for yourself, the pros and cons are mentioned at:
28
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
29
+ #
30
+ # vendor/Pods/
31
+
32
+ ## Documentation cache and generated files:
33
+ /.yardoc/
34
+ /_yardoc/
35
+ /doc/
36
+ /rdoc/
37
+
38
+ ## Environment normalization:
39
+ /.bundle/
40
+ /vendor/bundle
41
+ /lib/bundler/man/
42
+
43
+ # for a library or gem, you might want to ignore these files since the code is
44
+ # intended to run in multiple environments; otherwise, check them in:
45
+ # Gemfile.lock
46
+ # .ruby-version
47
+ # .ruby-gemset
48
+
49
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
50
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
4
+
@@ -0,0 +1,32 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ns-macro-processor (0.0.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rspec (3.7.0)
11
+ rspec-core (~> 3.7.0)
12
+ rspec-expectations (~> 3.7.0)
13
+ rspec-mocks (~> 3.7.0)
14
+ rspec-core (3.7.1)
15
+ rspec-support (~> 3.7.0)
16
+ rspec-expectations (3.7.0)
17
+ diff-lcs (>= 1.2.0, < 2.0)
18
+ rspec-support (~> 3.7.0)
19
+ rspec-mocks (3.7.0)
20
+ diff-lcs (>= 1.2.0, < 2.0)
21
+ rspec-support (~> 3.7.0)
22
+ rspec-support (3.7.0)
23
+
24
+ PLATFORMS
25
+ ruby
26
+
27
+ DEPENDENCIES
28
+ ns-macro-processor!
29
+ rspec (~> 3.7)
30
+
31
+ BUNDLED WITH
32
+ 1.16.1
@@ -0,0 +1,5 @@
1
+ .PHONY: spec
2
+
3
+ spec:
4
+ bundle exec rspec -Ilib
5
+
@@ -0,0 +1,79 @@
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
+
@@ -0,0 +1,10 @@
1
+ module MacroProcessor
2
+
3
+ class ProcessorError < StandardError
4
+ def initialize(field_name)
5
+ super("Error parsing template. No value supplied for field '#{field_name}'.")
6
+ end
7
+ end
8
+
9
+ end
10
+
@@ -0,0 +1,10 @@
1
+ module MacroProcessor
2
+
3
+ class SyntaxError < StandardError
4
+ def initialize(expected, actual)
5
+ super("Syntax error: expected '#{expected}' but was '#{actual}'")
6
+ end
7
+ end
8
+
9
+ end
10
+
@@ -0,0 +1,59 @@
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
+
@@ -0,0 +1,20 @@
1
+ # coding: utf-8
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'ns-macro-processor'
5
+ spec.version = '0.0.1'
6
+ spec.licenses = ['MIT']
7
+ spec.authors = ['Kevin Rutherford']
8
+ spec.email = ['kevin@rutherford-software.com']
9
+
10
+ spec.summary = %q{An opinionated macro processor}
11
+ spec.description = %q{An opinionated macro processor}
12
+ spec.homepage = "https://github.com/kevinrutherford/ns-macro-processor"
13
+
14
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^spec/}) }
15
+ spec.require_paths = ['lib']
16
+
17
+ spec.add_development_dependency 'rspec', '~> 3.7'
18
+
19
+ end
20
+
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ns-macro-processor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kevin Rutherford
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-02-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.7'
27
+ description: An opinionated macro processor
28
+ email:
29
+ - kevin@rutherford-software.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - Gemfile
36
+ - Gemfile.lock
37
+ - Makefile
38
+ - lib/macro_processor.rb
39
+ - lib/processor_error.rb
40
+ - lib/syntax_error.rb
41
+ - lib/tokens.rb
42
+ - ns-macro-processor.gemspec
43
+ homepage: https://github.com/kevinrutherford/ns-macro-processor
44
+ licenses:
45
+ - MIT
46
+ metadata: {}
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubyforge_project:
63
+ rubygems_version: 2.6.8
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: An opinionated macro processor
67
+ test_files: []