en_route 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +6 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +20 -0
- data/Rakefile +2 -0
- data/en_route.gemspec +19 -0
- data/lib/en_route.rb +6 -0
- data/lib/en_route/compiler.rb +44 -0
- data/lib/en_route/parser.rb +84 -0
- data/lib/en_route/tokens.rb +27 -0
- data/lib/en_route/version.rb +3 -0
- metadata +66 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Andrew Brown
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
data/en_route.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "en_route/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'en_route'
|
7
|
+
s.version = EnRoute::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ['Andrew WC Brown']
|
10
|
+
s.email = ['omen.king@gmail.com']
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{A a small off-side langauge that compiles into the routes.rb file for Rails 3}
|
13
|
+
s.description = %q{A a small off-side langauge that compiles into the routes.rb file for Rails 3}
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
end
|
data/lib/en_route.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'json'
|
2
|
+
module EnRoute
|
3
|
+
class Compiler
|
4
|
+
attr_accessor :file
|
5
|
+
|
6
|
+
def stab i, line
|
7
|
+
self.file.write "\n"+(1..i).collect{|i|"\s\s"}.join('')+line
|
8
|
+
end
|
9
|
+
|
10
|
+
def compile data, dir
|
11
|
+
self.file = File.open "#{dir}/routes.rb", 'w'
|
12
|
+
i = 0
|
13
|
+
self.file.write "## This file was generated by en_route, modify routes.rr instead of this file."
|
14
|
+
data.each do |e|
|
15
|
+
case e[0]
|
16
|
+
when :namespace; stab i, "namespace :#{e[1][:name]}"
|
17
|
+
when :resources; stab i, "resources :#{e[1][:name]}"
|
18
|
+
when :resource; stab i, "resource :#{e[1][:name]}"
|
19
|
+
when :collection; stab i, "collection"
|
20
|
+
when :member; stab i, "member"
|
21
|
+
when :post; stab i, "post :#{e[1][:name]}"
|
22
|
+
when :get; stab i, "get :#{e[1][:name]}"
|
23
|
+
when :root; self.file.write "root => '#{e[1][:to]}'"
|
24
|
+
when :indent; i = i.succ; self.file.write ' do'
|
25
|
+
when :dedent; i = i.pred; stab i, "end"
|
26
|
+
when :ruby; self.file.write "\n#{e[1]}"
|
27
|
+
when :match
|
28
|
+
params = nil
|
29
|
+
if e[1][:params]
|
30
|
+
p = e[1][:params].strip.gsub(/(\w+)\s*:/, '"\1":')
|
31
|
+
p.gsub!(/'/,'"')
|
32
|
+
p = JSON.parse(p)
|
33
|
+
p = p.inject({}){|m,(k,v)|m[k.to_sym]=v;m}
|
34
|
+
end
|
35
|
+
via = e[1][:via] == ':' ? 'match' : e[1][:via]
|
36
|
+
l = "#{via} '/#{e[1][:path]}' => '#{e[1][:to]}', :as => '#{e[1][:as]}'"
|
37
|
+
l = l+','+p.inspect if p
|
38
|
+
stab i, l
|
39
|
+
end
|
40
|
+
end
|
41
|
+
self.file.close
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
module EnRoute
|
2
|
+
class Parser
|
3
|
+
include Tokens
|
4
|
+
attr_accessor :ilevel, :istack, :data, :src_dir
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
self.ilevel = 0
|
8
|
+
self.data = []
|
9
|
+
self.src_dir = nil
|
10
|
+
end
|
11
|
+
|
12
|
+
def parse file_name
|
13
|
+
self.ilevel = 0
|
14
|
+
self.src_dir = File.dirname(file_name)
|
15
|
+
exec = false
|
16
|
+
exec_i = 0
|
17
|
+
f = File.open file_name, 'r'
|
18
|
+
f.lines do |line|
|
19
|
+
exec = exec && add_ruby_code?(exec_i,line)
|
20
|
+
unless exec
|
21
|
+
if line =~ Regexp.new(NAMESPACE); dump f, line, $1, [:namespace,{:name => $2}]
|
22
|
+
elsif line =~ Regexp.new(MATCH); dump f, line, $1, [:match,{:as => $3, :to => $4, :path => $5, :via => $2, :params => $6 }]
|
23
|
+
elsif line =~ Regexp.new(RESOURCES); dump f, line, $1, [:resources,{:name => $2}]
|
24
|
+
elsif line =~ Regexp.new(RESOURCE); dump f, line, $1, [:resource,{:name => $2}]
|
25
|
+
elsif line =~ Regexp.new(COLLECTION); dump f, line, $1, [:collection]
|
26
|
+
elsif line =~ Regexp.new(MEMBER); dump f, line, $1, [:member]
|
27
|
+
elsif line =~ Regexp.new(POST); dump f, line, $1, [:post,{:name => $2}]
|
28
|
+
elsif line =~ Regexp.new(ROOT); dump f, line, '', [:root,{:to => $1}]
|
29
|
+
elsif line =~ Regexp.new(GET); dump f, line, $1, [:get,{:name => $2}]
|
30
|
+
elsif line =~ Regexp.new(INCLUDE); include_script $1, $2
|
31
|
+
elsif line =~ Regexp.new(BLANK); #self.data << [:blank]
|
32
|
+
elsif line =~ Regexp.new(RUBY); exec = true; exec_i = $1.to_s.size
|
33
|
+
else; error 'token not found', f, line
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
indenting
|
38
|
+
f.close
|
39
|
+
self.data
|
40
|
+
end
|
41
|
+
|
42
|
+
def add_ruby_code? exec_i, line
|
43
|
+
i = line.match(/^\s*/).to_s.size
|
44
|
+
if i <= exec_i
|
45
|
+
false
|
46
|
+
else
|
47
|
+
self.data << [:ruby,line.sub(/^\s\s/,'')]
|
48
|
+
true
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def include_script tab, name
|
53
|
+
tab = tab.to_s.size
|
54
|
+
error('indenting must be 2 spaces:',f,line) if tab % 2 != 0
|
55
|
+
indenting tab
|
56
|
+
ilevel = self.ilevel
|
57
|
+
parse File.join(self.src_dir, "#{name}.rr")
|
58
|
+
self.ilevel = ilevel
|
59
|
+
end
|
60
|
+
|
61
|
+
def indenting tab=0
|
62
|
+
if tab - self.ilevel == 2
|
63
|
+
self.data << [:indent]
|
64
|
+
self.ilevel = tab
|
65
|
+
elsif tab < self.ilevel
|
66
|
+
(1..((self.ilevel-tab)/2)).each do
|
67
|
+
self.data << [:dedent]
|
68
|
+
end
|
69
|
+
self.ilevel = tab
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def dump(f,line,tab,item=nil)
|
74
|
+
tab = tab.to_s.size
|
75
|
+
error('indenting must be 2 spaces:',f,line) if tab % 2 != 0
|
76
|
+
indenting tab
|
77
|
+
self.data << item if item
|
78
|
+
end
|
79
|
+
|
80
|
+
def error(message,f,line)
|
81
|
+
raise "routie: #{message} #{f.inspect} on line\n\t#{f.lineno}: #{line}\n #{self.inspect}"
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module EnRoute
|
2
|
+
module Tokens
|
3
|
+
PARAMS = '(\s+\{\s*\w+\s*:\s*\'?\w+\'?\s*\})?'
|
4
|
+
INDENT = '^(\s*)'
|
5
|
+
EOL = '\s*$'
|
6
|
+
METHOD = '(\s+:\w+)?'
|
7
|
+
TO = '(\w+#\w+)'
|
8
|
+
PATH = '([\w|\/|:]+)'
|
9
|
+
AS = '(\w+|\/)'
|
10
|
+
NAME = '(\w+)'
|
11
|
+
SPACE = '\s'
|
12
|
+
SPACES = '\s+'
|
13
|
+
MATCHES = '(:\s*|get\s+|post\s+|put\s+|delete\s+|)'
|
14
|
+
RUBY = '^ruby:\s*$'
|
15
|
+
INCLUDE = [ INDENT , '@(\w+)', EOL].join
|
16
|
+
NAMESPACE = [ INDENT, 'namespace' , SPACE, NAME, EOL ].join
|
17
|
+
MATCH = [ INDENT, MATCHES, AS, SPACES, TO, SPACES, PATH, PARAMS, EOL ].join
|
18
|
+
RESOURCES = [ INDENT, 'resources' , SPACE, NAME, PARAMS, EOL ].join
|
19
|
+
RESOURCE = [ INDENT, 'resource' , SPACE, NAME, PARAMS, EOL ].join
|
20
|
+
COLLECTION = [ INDENT, 'collection' , EOL ].join
|
21
|
+
MEMBER = [ INDENT, 'member', EOL ].join
|
22
|
+
POST = [ INDENT, 'post', SPACES, NAME, EOL ].join
|
23
|
+
GET = [ INDENT, 'get', SPACES, NAME, EOL ].join
|
24
|
+
ROOT = ['^','root',SPACES,TO].join
|
25
|
+
BLANK = '^\s*$'
|
26
|
+
end
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: en_route
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Andrew WC Brown
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-06-03 00:00:00 -04:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: A a small off-side langauge that compiles into the routes.rb file for Rails 3
|
18
|
+
email:
|
19
|
+
- omen.king@gmail.com
|
20
|
+
executables: []
|
21
|
+
|
22
|
+
extensions: []
|
23
|
+
|
24
|
+
extra_rdoc_files: []
|
25
|
+
|
26
|
+
files:
|
27
|
+
- .gitignore
|
28
|
+
- Gemfile
|
29
|
+
- LICENSE.txt
|
30
|
+
- Rakefile
|
31
|
+
- en_route.gemspec
|
32
|
+
- lib/en_route.rb
|
33
|
+
- lib/en_route/compiler.rb
|
34
|
+
- lib/en_route/parser.rb
|
35
|
+
- lib/en_route/tokens.rb
|
36
|
+
- lib/en_route/version.rb
|
37
|
+
has_rdoc: true
|
38
|
+
homepage: ""
|
39
|
+
licenses: []
|
40
|
+
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: "0"
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
requirements: []
|
59
|
+
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 1.6.2
|
62
|
+
signing_key:
|
63
|
+
specification_version: 3
|
64
|
+
summary: A a small off-side langauge that compiles into the routes.rb file for Rails 3
|
65
|
+
test_files: []
|
66
|
+
|