jejune 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,81 @@
1
+ #!/usr/bin/ruby
2
+ # encoding: utf-8
3
+ #--
4
+ # Copyright (c) 2010-2011 Kyle C. Yetter
5
+ #
6
+ # Permission is hereby granted, free of charge, to any person obtaining
7
+ # a copy of this software and associated documentation files (the
8
+ # "Software"), to deal in the Software without restriction, including
9
+ # without limitation the rights to use, copy, modify, merge, publish,
10
+ # distribute, sublicense, and/or sell copies of the Software, and to
11
+ # permit persons to whom the Software is furnished to do so, subject to
12
+ # the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be
15
+ # included in all copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
+ #++
25
+
26
+
27
+ module Jejune
28
+ class TreeWalker
29
+ include TokenData
30
+
31
+ def self.on( *types, &block )
32
+ types.each do | type |
33
+ name = Integer === type ? TOKEN_NAMES[ type ].downcase : type.to_s
34
+ define_method( "on_#{ name }", &block )
35
+ end
36
+ end
37
+
38
+ def self.skip_list
39
+ @skip_list ||= Set.new
40
+ end
41
+
42
+ def self.skip( *types )
43
+ types = [ types ].flatten!
44
+ skip_list.merge( types )
45
+ end
46
+
47
+ def enter( node )
48
+ handler = @handlers[ node.type ] and handler.call( node )
49
+ ensure
50
+ $! and $!.jejune_trace.push( trace_string( node ) )
51
+ end
52
+
53
+ def default( node )
54
+ for child in node do enter( child ) end
55
+ end
56
+
57
+ def descend( node )
58
+ for child in node
59
+ enter( child ) unless @skip_list.include?( child.type )
60
+ end
61
+ end
62
+
63
+ def initialize
64
+ token_names = TokenData::TOKEN_NAMES
65
+ default = method( :default )
66
+ @skip_list = self.class.skip_list
67
+ @handlers = {}
68
+ for type, name in token_names
69
+ method_name = "on_#{ name.downcase }"
70
+ method = respond_to?( method_name ) ? method( method_name ) : default
71
+ @handlers[ type ] = method
72
+ end
73
+ end
74
+
75
+ def trace_string( node )
76
+ file = node.source_name || '(input)'
77
+ line = node.line
78
+ "#{ file }:#{ line }"
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,81 @@
1
+ #!/usr/bin/ruby
2
+ # encoding: utf-8
3
+ #--
4
+ # Copyright (c) 2010-2011 Kyle C. Yetter
5
+ #
6
+ # Permission is hereby granted, free of charge, to any person obtaining
7
+ # a copy of this software and associated documentation files (the
8
+ # "Software"), to deal in the Software without restriction, including
9
+ # without limitation the rights to use, copy, modify, merge, publish,
10
+ # distribute, sublicense, and/or sell copies of the Software, and to
11
+ # permit persons to whom the Software is furnished to do so, subject to
12
+ # the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be
15
+ # included in all copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
+ #++
25
+
26
+ module Jejune
27
+
28
+ module Utils
29
+ JJS_EXTS = %w( .jjs )
30
+
31
+ module_function
32
+
33
+ def find_in_directory( directory, name, exts = '' )
34
+ exts = parse_ext_list( exts )
35
+ if block_given?
36
+ for ext in exts
37
+ path = File.join( directory, "#{ name }#{ ext }" )
38
+ yield( path ) and return path
39
+ end
40
+ else
41
+ for ext in exts
42
+ path = File.join( directory, "#{ name }#{ ext }" )
43
+ test( ?f, path ) and return path
44
+ end
45
+ end
46
+ return nil
47
+ end
48
+
49
+ def find_in_path_list( list, name, *exts, &b )
50
+ for dir in list
51
+ found = find_in_directory( dir, name, *exts, &b ) and return found
52
+ end
53
+ return nil
54
+ end
55
+
56
+ def parse_ext_list( exts )
57
+ exts = exts.to_s.strip.split( /[^\w\.]/, -1 ).
58
+ map! { | i | i.empty? ? i : i.start_with?( '.' ) ? i : ".#{ i }" }
59
+ exts.empty? and exts << ''
60
+ return exts
61
+ end
62
+
63
+ def jejune_file?( path )
64
+ JJS_EXTS.include?( File.extname( path ) )
65
+ end
66
+
67
+ def ArgumentError( required, optional, given )
68
+ min = required
69
+ max = optional >= 0 ? min + optional : nil
70
+
71
+ if given >= min and max and given <= max
72
+ warn( "called ArgumentError.build with argument specs that shouldn't result in an error" )
73
+ elsif given < min
74
+ new( "too few arguments (#{ given } for #{ min })" )
75
+ elsif max and given > max
76
+ new( "too many arguments (#{ given } for #{ max })" )
77
+ end
78
+ end
79
+ end
80
+
81
+ end
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/ruby
2
+ # encoding: utf-8
3
+ #--
4
+ # Copyright (c) 2010-2011 Kyle C. Yetter
5
+ #
6
+ # Permission is hereby granted, free of charge, to any person obtaining
7
+ # a copy of this software and associated documentation files (the
8
+ # "Software"), to deal in the Software without restriction, including
9
+ # without limitation the rights to use, copy, modify, merge, publish,
10
+ # distribute, sublicense, and/or sell copies of the Software, and to
11
+ # permit persons to whom the Software is furnished to do so, subject to
12
+ # the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be
15
+ # included in all copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
+ #++
25
+
26
+
27
+ module Jejune
28
+
29
+ #
30
+ # The version data for the current state the library itself
31
+ #
32
+ MAJOR_VERSION = 1
33
+ MINOR_VERSION = 1
34
+ PATCH_VERSION = 0
35
+
36
+ VERSION = [ MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION ].join( '.' ).freeze
37
+
38
+ end
@@ -0,0 +1,51 @@
1
+ <=[ Strings ]=>
2
+ `
3
+ | <body>
4
+ | <p>JavaScript wants your strings to sit on a single line.</p>
5
+ | </body>
6
+ `
7
+ ---
8
+ "What's so damn terrible about\
9
+ multiple-line strings, JavaScript?
10
+ huh?"
11
+ ---
12
+ "interpolate #{ values.join( "" ) } with braces"
13
+ ---
14
+ 'do not interpolate in #{ single } quotes'
15
+ ---
16
+ %q(<a href="javascript:void(0);">click me</a>)
17
+ ---
18
+ %w( aquamarine turquoise burnt\ sienna )
19
+ ---
20
+ "\n\t\r\0\xFF\"\'\\"
21
+ ---
22
+ '\n\t\r\0\xFF\"\'\\'
23
+ ---
24
+ %y(
25
+ a: 1
26
+ b: 2
27
+ )
28
+ ---
29
+ "abc\#{ d }e"
30
+ ---
31
+ @count = 0
32
+ ---
33
+ var x = @to_s()
34
+ ---
35
+ var i = 0
36
+ until ( i > 5 ) {
37
+ i++
38
+ }
39
+ ---
40
+ unless ( text.match( /^\w+$/ ) ) throw( new ArgumentError( "bad name" ) )
41
+ ---
42
+ action = ->(x) { x + 1 }
43
+ ---
44
+ [ 1, 2, 3 ].map { | i | i * i }
45
+ ---
46
+ new Ajax.Request( '/your/url',
47
+ type: 'GET',
48
+ onSuccess ->( transport ) {
49
+ transport.headerJSON
50
+ }
51
+ )
@@ -0,0 +1,69 @@
1
+ #!/usr/bin/ruby
2
+ # encoding: utf-8
3
+ #
4
+ # author: Kyle Yetter
5
+ #
6
+
7
+ require 'spec'
8
+ require 'jejune'
9
+
10
+ describe Jejune, 'translation' do
11
+ def self.translation( source, expected_output )
12
+ example( source ) do
13
+ output = Jejune::JJSSource.new( source ).translate.strip
14
+ expected_output.strip!
15
+ output.should == expected_output
16
+ end
17
+ end
18
+
19
+ translation "@run( 1, 2, 3 )", 'this.run( 1, 2, 3 )'
20
+
21
+ translation "@prop.thing = [ 1, 2, 3 ]", 'this.prop.thing = [ 1, 2, 3 ]'
22
+
23
+ translation "a[ 3 ] ||= x + 4", "a[ 3 ] = a[ 3 ] || x + 4"
24
+
25
+ translation "@y ||= @name.trim()", "this.y = this.y || this.name.trim()"
26
+
27
+ translation %(@y ||= "string" == typeof( x ) ? x : x.toString()),
28
+ %(this.y = this.y || ( "string" == typeof( x ) ? x : x.toString() ))
29
+
30
+ translation(
31
+ %q(
32
+ `
33
+ | <body>
34
+ | <p>JavaScript wants your strings to sit on a single line.</p>
35
+ | </body>
36
+ `
37
+ ),
38
+ %q("<body>\n <p>JavaScript wants your strings to sit on a single line.</p>\n</body>")
39
+ )
40
+
41
+ translation(
42
+ %q(
43
+ var x =
44
+ " What's so damn terrible about
45
+ multiple-line strings, JavaScript?\
46
+ ".trim().replace( /\s+/, ' ' )
47
+ ),
48
+ %q(
49
+ var x =
50
+ " What's so damn terrible about\n multiple-line strings, JavaScript? ".trim().replace( /\s+/, ' ' )
51
+ )
52
+ )
53
+
54
+ translation "[ 1, 2, 10 ].sort { | a, b | a < b ? -1 : a == b ? 0 : 1 }",
55
+ "[ 1, 2, 10 ].sort( function( a, b ) { return( a < b ? -1 : a == b ? 0 : 1 ); } )"
56
+
57
+ translation "-> { 4 + 5; }", "function() { return( 4 + 5 ); }"
58
+
59
+ translation "$( 'button' ).insert( below: el )",
60
+ "$( 'button' ).insert( { below: el } )"
61
+
62
+ translation %q(%w( a bunch of words and one\ space )),
63
+ %q([ "a", "bunch", "of", "words", "and", "one space" ])
64
+
65
+ translation %q( "<p class=#{ cName.quote() }>#{ content }</p>" ),
66
+ %q( [ "<p class=", cName.quote(), ">", content, "</p>" ].join( '' ) )
67
+
68
+ end
69
+
@@ -0,0 +1,63 @@
1
+ #!/usr/bin/ruby
2
+ # encoding: utf-8
3
+
4
+ =begin ::about::
5
+ author: Kyle Yetter <kcy5b@yahoo.com>
6
+ created on: February 05, 2011
7
+ purpose: (program | library | utility script | ?)
8
+ summary:
9
+ loads: files required by this
10
+ autoloads: autoload entries in this (e.g. YAML(yaml))
11
+ =end
12
+
13
+ require 'jejune'
14
+ require 'fileutils'
15
+
16
+ THIS_DIR = File.dirname( __FILE__ )
17
+ SAMPLE_DIR = File.join( THIS_DIR, "sample-dirs" )
18
+ SAMPLE_TREE = %w(
19
+ a/x
20
+ a/x.js
21
+ z.jjs
22
+ b/y.jjs
23
+ b/x.jjs
24
+ )
25
+
26
+
27
+ describe Jejune::Utils do
28
+ include FileUtils
29
+ include FileTest
30
+ include Jejune::Utils
31
+
32
+ def path( *args ) File.join( SAMPLE_DIR, *args ) end
33
+ def glob( *args ) Dir.glob( path( *args ) ) end
34
+
35
+ before :all do
36
+ @base = SAMPLE_DIR
37
+ mkpath( @base )
38
+ for pt in SAMPLE_TREE
39
+ pt = path( pt )
40
+ mkpath( File.dirname( pt ) )
41
+ touch pt
42
+ end
43
+
44
+ @search = %w( a b ).map { | i | path( i ) }
45
+
46
+ end
47
+
48
+ after :all do
49
+ directory?( SAMPLE_DIR ) and rm_r( SAMPLE_DIR )
50
+ end
51
+
52
+ example 'find_in_directory' do
53
+ find_in_directory( @base, 'z' ).should be_nil
54
+ find_in_directory( @base, 'z', 'jjs' ).should == path( 'z.jjs' )
55
+ end
56
+
57
+ example 'find_in_path_list' do
58
+ find_in_path_list( @search, 'x.jjs' ).should == path( 'b/x.jjs' )
59
+ find_in_path_list( @search, 'x' ).should == path( 'a/x' )
60
+ end
61
+
62
+ end
63
+
@@ -0,0 +1,2 @@
1
+ push-path bin
2
+ push-path scripts
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jejune
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kyle Yetter
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: antlr3
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sass
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '3.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '3.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rdoc
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '4.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '4.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: hoe
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '3.7'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '3.7'
69
+ description: Make JavaScript look and feel like Ruby -- fancy JavaScript preprocessor.
70
+ More info to come later.
71
+ email:
72
+ - kyle@ohboyohboyohboy.org
73
+ executables:
74
+ - jjs
75
+ extensions: []
76
+ extra_rdoc_files:
77
+ - History.txt
78
+ - Manifest.txt
79
+ - README.txt
80
+ files:
81
+ - lib/jejune.rb
82
+ - lib/jejune/ejjs.rb
83
+ - lib/jejune/rewrite-debug.rb
84
+ - lib/jejune/errors.rb
85
+ - lib/jejune/node-test.rb
86
+ - lib/jejune/tree-walker.rb
87
+ - lib/jejune/main.rb
88
+ - lib/jejune/jstring.rb
89
+ - lib/jejune/grammar.rb
90
+ - lib/jejune/translator.rb
91
+ - lib/jejune/version.rb
92
+ - lib/jejune/dependency-scanner.rb
93
+ - lib/jejune/grammar/Jejune.g
94
+ - lib/jejune/grammar/rakefile
95
+ - lib/jejune/grammar/tree.rb
96
+ - lib/jejune/grammar/lexer.rb
97
+ - lib/jejune/grammar/Jejune.tokens
98
+ - lib/jejune/grammar/parser.rb
99
+ - lib/jejune/grammar/JavaScript.g
100
+ - lib/jejune/lo-fi-lexer.rb
101
+ - lib/jejune/manager.rb
102
+ - lib/jejune/scanner.rb
103
+ - lib/jejune/utils.rb
104
+ - lib/jejune/input.rb
105
+ - lib/jejune/macro.rb
106
+ - lib/jejune/boot.rb
107
+ - lib/jejune/constants.rb
108
+ - lib/jejune/rewrite.rb
109
+ - lib/jejune/data-extension.rb
110
+ - lib/jejune/parameters.rb
111
+ - History.txt
112
+ - Manifest.txt
113
+ - Rakefile
114
+ - README.txt
115
+ - bin/jjs
116
+ - spec/samples.txt
117
+ - spec/translation.rb
118
+ - spec/utils.rb
119
+ - tools/env.fish
120
+ - .gemtest
121
+ homepage: http://www.jjscript.com
122
+ licenses:
123
+ - MIT
124
+ metadata: {}
125
+ post_install_message:
126
+ rdoc_options:
127
+ - --main
128
+ - README.txt
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ! '>='
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ! '>='
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ requirements: []
142
+ rubyforge_project: jejune
143
+ rubygems_version: 2.0.3
144
+ signing_key:
145
+ specification_version: 4
146
+ summary: Make JavaScript look and feel like Ruby -- fancy JavaScript preprocessor
147
+ test_files: []