parsec 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/Guardfile +3 -0
- data/LICENSE.txt +23 -0
- data/README.md +51 -0
- data/Rakefile +1 -0
- data/lib/parsec.rb +2 -0
- data/lib/parsec/pos.rb +98 -0
- data/lib/parsec/version.rb +3 -0
- data/parsec.gemspec +25 -0
- data/spec/parsec/pos_spec.rb +87 -0
- data/spec/parsec_spec.rb +7 -0
- data/spec/spec_helper.rb +6 -0
- metadata +117 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
Copyright (c) 2013 Utkarsh Kukreti.
|
2
|
+
|
3
|
+
Copyright 1999-2000, Daan Leijen; 2007, Paolo Martini. All rights reserved.
|
4
|
+
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
7
|
+
|
8
|
+
* Redistributions of source code must retain the above copyright notice,
|
9
|
+
this list of conditions and the following disclaimer.
|
10
|
+
* Redistributions in binary form must reproduce the above copyright
|
11
|
+
notice, this list of conditions and the following disclaimer in the
|
12
|
+
documentation and/or other materials provided with the distribution.
|
13
|
+
|
14
|
+
This software is provided by the copyright holders "as is" and any express or
|
15
|
+
implied warranties, including, but not limited to, the implied warranties of
|
16
|
+
merchantability and fitness for a particular purpose are disclaimed. In no
|
17
|
+
event shall the copyright holders be liable for any direct, indirect,
|
18
|
+
incidental, special, exemplary, or consequential damages (including, but not
|
19
|
+
limited to, procurement of substitute goods or services; loss of use, data,
|
20
|
+
or profits; or business interruption) however caused and on any theory of
|
21
|
+
liability, whether in contract, strict liability, or tort (including
|
22
|
+
negligence or otherwise) arising in any way out of the use of this software,
|
23
|
+
even if advised of the possibility of such damage.
|
data/README.md
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# Parsec \[WIP\]
|
2
|
+
|
3
|
+
A one-to-one translation of Haskell's
|
4
|
+
[Parsec](http://www.haskell.org/haskellwiki/Parsec) library to Ruby.
|
5
|
+
|
6
|
+
This would be horribly inefficient as Ruby is /not/ optimized for functional
|
7
|
+
use.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
$ git clone https://github.com/utkarshkukreti/parsec.rb.git
|
12
|
+
$ cd parsec.rb
|
13
|
+
$ bundle install
|
14
|
+
$ rake install
|
15
|
+
|
16
|
+
## Usage
|
17
|
+
|
18
|
+
Require the library
|
19
|
+
|
20
|
+
require "parsec"
|
21
|
+
|
22
|
+
All functions are present in the `Parsec` module. You can just `extend Parsec`
|
23
|
+
in your code for testing, or include it in a class, and use instances of it.
|
24
|
+
|
25
|
+
## License
|
26
|
+
|
27
|
+
Copyright (c) 2013 Utkarsh Kukreti.
|
28
|
+
|
29
|
+
All code is licensed under the same license as Parsec's license, which is:
|
30
|
+
|
31
|
+
Copyright 1999-2000, Daan Leijen; 2007, Paolo Martini. All rights reserved.
|
32
|
+
|
33
|
+
Redistribution and use in source and binary forms, with or without
|
34
|
+
modification, are permitted provided that the following conditions are met:
|
35
|
+
|
36
|
+
* Redistributions of source code must retain the above copyright notice,
|
37
|
+
this list of conditions and the following disclaimer.
|
38
|
+
* Redistributions in binary form must reproduce the above copyright
|
39
|
+
notice, this list of conditions and the following disclaimer in the
|
40
|
+
documentation and/or other materials provided with the distribution.
|
41
|
+
|
42
|
+
This software is provided by the copyright holders "as is" and any express or
|
43
|
+
implied warranties, including, but not limited to, the implied warranties of
|
44
|
+
merchantability and fitness for a particular purpose are disclaimed. In no
|
45
|
+
event shall the copyright holders be liable for any direct, indirect,
|
46
|
+
incidental, special, exemplary, or consequential damages (including, but not
|
47
|
+
limited to, procurement of substitute goods or services; loss of use, data,
|
48
|
+
or profits; or business interruption) however caused and on any theory of
|
49
|
+
liability, whether in contract, strict liability, or tort (including
|
50
|
+
negligence or otherwise) arising in any way out of the use of this software,
|
51
|
+
even if advised of the possibility of such damage.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/parsec.rb
ADDED
data/lib/parsec/pos.rb
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
module Parsec
|
2
|
+
def SourceName(value)
|
3
|
+
value.to_s
|
4
|
+
end
|
5
|
+
|
6
|
+
def Line(value)
|
7
|
+
value.to_i
|
8
|
+
end
|
9
|
+
|
10
|
+
def Column(value)
|
11
|
+
value.to_i
|
12
|
+
end
|
13
|
+
|
14
|
+
def SourcePos(source_name, line = nil, column = nil)
|
15
|
+
source_name, line, column = source_name, line, column
|
16
|
+
Object.new.tap do |object|
|
17
|
+
object.instance_variable_set "@data", [source_name, line, column]
|
18
|
+
|
19
|
+
def object.[](index)
|
20
|
+
@data[index]
|
21
|
+
end
|
22
|
+
|
23
|
+
def object.to_s
|
24
|
+
if self[0].nil?
|
25
|
+
show_line_column
|
26
|
+
else
|
27
|
+
"\"#{self[0]}\" " + show_line_column
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def object.inspect
|
32
|
+
to_s
|
33
|
+
end
|
34
|
+
|
35
|
+
def object.show_line_column
|
36
|
+
"(line #{self[1]}, column #{self[2]})"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def newPos(name, line, column)
|
42
|
+
SourcePos(name, line, column)
|
43
|
+
end
|
44
|
+
|
45
|
+
def initialPos(name)
|
46
|
+
SourcePos(name, 1, 1)
|
47
|
+
end
|
48
|
+
|
49
|
+
def sourceName(source_pos)
|
50
|
+
source_pos[0]
|
51
|
+
end
|
52
|
+
|
53
|
+
def sourceLine(source_pos)
|
54
|
+
source_pos[1]
|
55
|
+
end
|
56
|
+
|
57
|
+
def sourceColumn(source_pos)
|
58
|
+
source_pos[2]
|
59
|
+
end
|
60
|
+
|
61
|
+
def incSourceLine(source_pos, n)
|
62
|
+
SourcePos(source_pos[0], source_pos[1] + n, source_pos[2])
|
63
|
+
end
|
64
|
+
|
65
|
+
def incSourceColumn(source_pos, n)
|
66
|
+
SourcePos(source_pos[0], source_pos[1], source_pos[2] + n)
|
67
|
+
end
|
68
|
+
|
69
|
+
def setSourceName(source_pos, n)
|
70
|
+
SourcePos(n, source_pos[1], source_pos[2])
|
71
|
+
end
|
72
|
+
|
73
|
+
def setSourceLine(source_pos, n)
|
74
|
+
SourcePos(source_pos[0], n, source_pos[2])
|
75
|
+
end
|
76
|
+
|
77
|
+
def setSourceColumn(source_pos, n)
|
78
|
+
SourcePos(source_pos[0], source_pos[1], n)
|
79
|
+
end
|
80
|
+
|
81
|
+
def updatePosString(pos, string)
|
82
|
+
string.chars.to_a.reduce(pos) do |acc, el|
|
83
|
+
updatePosChar acc, el
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def updatePosChar(source_pos, c)
|
88
|
+
case c
|
89
|
+
when "\n"
|
90
|
+
SourcePos(source_pos[0], source_pos[1] + 1, 1)
|
91
|
+
when "\t"
|
92
|
+
SourcePos(source_pos[0], source_pos[1],
|
93
|
+
source_pos[2] + 8 - ((source_pos[2] - 1) % 8))
|
94
|
+
else
|
95
|
+
SourcePos(source_pos[0], source_pos[1], source_pos[2] + 1)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
data/parsec.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'parsec/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "parsec"
|
8
|
+
gem.version = Parsec::VERSION
|
9
|
+
gem.authors = ["Utkarsh Kukreti"]
|
10
|
+
gem.email = ["utkarshkukreti@gmail.com"]
|
11
|
+
gem.description = "A one-to-one translation of Haskell’s Parsec library" +
|
12
|
+
" to Ruby."
|
13
|
+
gem.summary = gem.description
|
14
|
+
gem.homepage = ""
|
15
|
+
gem.license = "MIT"
|
16
|
+
|
17
|
+
gem.files = `git ls-files`.split($/)
|
18
|
+
gem.executables = gem.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
20
|
+
gem.require_paths = ["lib"]
|
21
|
+
|
22
|
+
%w{rake rspec guard-rspec}.each do |name|
|
23
|
+
gem.add_development_dependency name
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Parsec do
|
4
|
+
context "pos" do
|
5
|
+
context "SourceName" do
|
6
|
+
it "should have String as an ancestor" do
|
7
|
+
SourceName("").class.ancestors.should include String
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
context "Line" do
|
12
|
+
it "should have Integer as an ancestor" do
|
13
|
+
Line(1).class.ancestors.should include Integer
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
context "Column" do
|
18
|
+
it "should have Integer as an ancestor" do
|
19
|
+
Column(1).class.ancestors.should include Integer
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context "SourcePos" do
|
24
|
+
context "[]" do
|
25
|
+
subject { SourcePos("a", 1, 1) }
|
26
|
+
its([0]) { should eq "a" }
|
27
|
+
its([1]) { should eq 1 }
|
28
|
+
its([2]) { should eq 1 }
|
29
|
+
end
|
30
|
+
|
31
|
+
context ".to_s" do
|
32
|
+
it { SourcePos("a", 1, 1).to_s.should eq '"a" (line 1, column 1)'}
|
33
|
+
it { SourcePos(nil, 1, 1).to_s.should eq '(line 1, column 1)'}
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context "newPos" do
|
38
|
+
it "should delegate to SourcePos" do
|
39
|
+
a = newPos("", 1, 1)
|
40
|
+
b = SourcePos("", 1, 1)
|
41
|
+
a[0].should eq b[0]
|
42
|
+
a[1].should eq b[1]
|
43
|
+
a[2].should eq b[2]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context "initialPos" do
|
48
|
+
subject { initialPos "" }
|
49
|
+
|
50
|
+
its([0]) { should eq "" }
|
51
|
+
its([1]) { should eq 1 }
|
52
|
+
its([2]) { should eq 1 }
|
53
|
+
end
|
54
|
+
|
55
|
+
context "sourceName, sourceLine, sourceColumn" do
|
56
|
+
subject { SourcePos("", 1, 1) }
|
57
|
+
it { sourceName(subject).should eq "" }
|
58
|
+
it { sourceLine(subject).should eq 1 }
|
59
|
+
it { sourceColumn(subject).should eq 1 }
|
60
|
+
end
|
61
|
+
|
62
|
+
context "incSourceLine, incSourceColumn" do
|
63
|
+
subject { SourcePos("", 1, 2) }
|
64
|
+
it { sourceLine(incSourceLine(subject, 2)).should eq 3 }
|
65
|
+
it { sourceColumn(incSourceColumn(subject, 3)).should eq 5 }
|
66
|
+
end
|
67
|
+
|
68
|
+
context "setSourceName, setSourceLine, setSourceColumn" do
|
69
|
+
subject { SourcePos("", 1, 1) }
|
70
|
+
it { sourceName(setSourceName(subject, "a")).should eq "a" }
|
71
|
+
it { sourceLine(setSourceLine(subject, 2)).should eq 2 }
|
72
|
+
it { sourceColumn(setSourceColumn(subject, 3)).should eq 3 }
|
73
|
+
end
|
74
|
+
|
75
|
+
context "updatePosString" do
|
76
|
+
subject { updatePosString(SourcePos("", 1, 1), "a\nb\t\tc") }
|
77
|
+
it { sourceLine(subject).should eq 2 }
|
78
|
+
it { sourceColumn(subject).should eq 18 }
|
79
|
+
end
|
80
|
+
|
81
|
+
context "updatePosChar" do
|
82
|
+
subject { updatePosChar(SourcePos("", 1, 1), "a") }
|
83
|
+
it { sourceLine(subject).should eq 1 }
|
84
|
+
it { sourceColumn(subject).should eq 2 }
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
data/spec/parsec_spec.rb
ADDED
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: parsec
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Utkarsh Kukreti
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-08 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: guard-rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: A one-to-one translation of Haskell’s Parsec library to Ruby.
|
63
|
+
email:
|
64
|
+
- utkarshkukreti@gmail.com
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- .rspec
|
71
|
+
- Gemfile
|
72
|
+
- Guardfile
|
73
|
+
- LICENSE.txt
|
74
|
+
- README.md
|
75
|
+
- Rakefile
|
76
|
+
- lib/parsec.rb
|
77
|
+
- lib/parsec/pos.rb
|
78
|
+
- lib/parsec/version.rb
|
79
|
+
- parsec.gemspec
|
80
|
+
- spec/parsec/pos_spec.rb
|
81
|
+
- spec/parsec_spec.rb
|
82
|
+
- spec/spec_helper.rb
|
83
|
+
homepage: ''
|
84
|
+
licenses:
|
85
|
+
- MIT
|
86
|
+
post_install_message:
|
87
|
+
rdoc_options: []
|
88
|
+
require_paths:
|
89
|
+
- lib
|
90
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
91
|
+
none: false
|
92
|
+
requirements:
|
93
|
+
- - ! '>='
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
segments:
|
97
|
+
- 0
|
98
|
+
hash: -2798352916948011874
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
none: false
|
101
|
+
requirements:
|
102
|
+
- - ! '>='
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
segments:
|
106
|
+
- 0
|
107
|
+
hash: -2798352916948011874
|
108
|
+
requirements: []
|
109
|
+
rubyforge_project:
|
110
|
+
rubygems_version: 1.8.24
|
111
|
+
signing_key:
|
112
|
+
specification_version: 3
|
113
|
+
summary: A one-to-one translation of Haskell’s Parsec library to Ruby.
|
114
|
+
test_files:
|
115
|
+
- spec/parsec/pos_spec.rb
|
116
|
+
- spec/parsec_spec.rb
|
117
|
+
- spec/spec_helper.rb
|