lox 0.1.0

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
+ SHA256:
3
+ metadata.gz: ffb42bd45a5bb10596114254b3bc9ed0094950b9465f16a32c350f39a5853404
4
+ data.tar.gz: dbcf5fa9bf2ccaeab7aea83adc360ae490a1462adfac1c8d0088bdaca4f45dd1
5
+ SHA512:
6
+ metadata.gz: 38ee4fd5f679a00dafd4494120cd0f6f1ea28ccdaa6cfb1e1b7289357d435d4a664cf1c7f97b6e65a260a93def553a47e5a61e82ffb2aa66699ad88c699b2893
7
+ data.tar.gz: 0772d262b061e20beb0473f8e14a518c047be4b980a04ff06ba8cc980602104549b4b36cb1f9bce2cc750fcd036befd8bffc75cc11bba59a901a8f8f8d8e0190
@@ -0,0 +1,28 @@
1
+ ---
2
+ engines:
3
+ bundler-audit:
4
+ enabled: true
5
+ duplication:
6
+ enabled: true
7
+ config:
8
+ languages:
9
+ - ruby
10
+ - javascript
11
+ - python
12
+ - php
13
+ fixme:
14
+ enabled: true
15
+ rubocop:
16
+ enabled: true
17
+ ratings:
18
+ paths:
19
+ - Gemfile.lock
20
+ - "**.inc"
21
+ - "**.js"
22
+ - "**.jsx"
23
+ - "**.module"
24
+ - "**.php"
25
+ - "**.py"
26
+ - "**.rb"
27
+ exclude_paths:
28
+ - spec/
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
@@ -0,0 +1,26 @@
1
+ require: rubocop-rspec
2
+
3
+ AllCops:
4
+ DisplayCopNames: true
5
+ EnabledByDefault: true
6
+ TargetRubyVersion: 2.2
7
+
8
+ Metrics/BlockLength:
9
+ Exclude:
10
+ - lox.gemspec
11
+ - spec/**/*_spec.rb
12
+
13
+ Style/Copyright:
14
+ Enabled: false
15
+
16
+ Style/Documentation:
17
+ Enabled: false
18
+
19
+ Style/DocumentationMethod:
20
+ Enabled: false
21
+
22
+ Style/FrozenStringLiteralComment:
23
+ Enabled: false
24
+
25
+ Style/MethodCallWithArgsParentheses:
26
+ Enabled: false
@@ -0,0 +1 @@
1
+ ruby-2.5.0
@@ -0,0 +1 @@
1
+ SimpleCov.start
@@ -0,0 +1,15 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.5
4
+ - 2.4
5
+ - 2.3
6
+ - 2.2
7
+ addons:
8
+ code_climate:
9
+ repo_token:
10
+ secure: hzkkfSIVGAsI0100Eca0u9vRoQaqO9hkLE+Q7geAVBcnFmiHj2ukNX2BoHr1AQ4OAYNuobNVUbFYbuwDGoLNi8Eocoo1ZQblM4rBAKm3QTe/NmRSwJfdqH716Bt7ttfBz4DIsCm+POKwdnRT820ltMS5pBBz3WEUPRS54mvbtd63I5gUuf761eO8n9mCYIuRSLVU/oFzz0C7eSapzDxyE0UQ46647WLbFOE01R+DBAlHTI1Ua2j1fAxxiktYf/mI+B9842I61gsWCh50oBw55CxgsX+IzrIgV/NcCzaGBRPDy/8glecNzgB4hRwBHT9LSMe6SzKjmVcVblHUTfffgrHUi+NKL67cSnZgNaj9JHHDQIeNBswRR7Z0duMo0/0nbg2EYjlL2QLhDrhzwFZVLQZX/XnKx6wckG3KFCNVjjPXg07xV4tPLtEOBmW20qG93PBG5vbkZZh6G4VwaNj543TNfaaCIn5Gj0TxA0bZ/Tk+dUpHZ+wTGJn4nS50rybceJHfRT7aiFG6lzIvzBMVyimWbliJfivKdtjPCi27Yip+g4p6TecSkOfTDlYYOfclsIkg8O0Fbu7zirqyk6KiBxK4ZvKIYYXgVZRCU2d6xrGMXwHPF9KgqtZLcxCLjdMJkIjFlp4SOiCWsD2QKZ420eKDPPEDepFFMvNoWNf6YlE=
11
+ before_install:
12
+ - gem update --system
13
+ - gem install bundler --conservative
14
+ after_success:
15
+ - bundle exec codeclimate-test-reporter
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at <richard.elias.dodson@gmail.com>. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in lox.gemspec
6
+ gemspec
@@ -0,0 +1,173 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ lox (0.1.0)
5
+ thor
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ abstract_type (0.0.7)
11
+ adamantium (0.2.0)
12
+ ice_nine (~> 0.11.0)
13
+ memoizable (~> 0.4.0)
14
+ addressable (2.4.0)
15
+ anima (0.3.0)
16
+ abstract_type (~> 0.0.7)
17
+ adamantium (~> 0.2)
18
+ equalizer (~> 0.0.11)
19
+ ast (2.3.0)
20
+ backports (3.11.0)
21
+ byebug (9.1.0)
22
+ codeclimate-test-reporter (1.0.7)
23
+ simplecov
24
+ coderay (1.1.2)
25
+ concord (0.1.5)
26
+ adamantium (~> 0.2.0)
27
+ equalizer (~> 0.0.9)
28
+ diff-lcs (1.3)
29
+ docile (1.1.5)
30
+ equalizer (0.0.11)
31
+ ethon (0.11.0)
32
+ ffi (>= 1.3.0)
33
+ faraday (0.13.1)
34
+ multipart-post (>= 1.2, < 3)
35
+ faraday_middleware (0.12.2)
36
+ faraday (>= 0.7.4, < 1.0)
37
+ ffi (1.9.18)
38
+ gh (0.15.1)
39
+ addressable (~> 2.4.0)
40
+ backports
41
+ faraday (~> 0.8)
42
+ multi_json (~> 1.0)
43
+ net-http-persistent (~> 2.9)
44
+ net-http-pipeline
45
+ highline (1.7.10)
46
+ ice_nine (0.11.2)
47
+ json (2.1.0)
48
+ launchy (2.4.3)
49
+ addressable (~> 2.3)
50
+ memoizable (0.4.2)
51
+ thread_safe (~> 0.3, >= 0.3.1)
52
+ method_source (0.9.0)
53
+ morpher (0.2.6)
54
+ abstract_type (~> 0.0.7)
55
+ adamantium (~> 0.2.0)
56
+ anima (~> 0.3.0)
57
+ ast (~> 2.2)
58
+ concord (~> 0.1.5)
59
+ equalizer (~> 0.0.9)
60
+ ice_nine (~> 0.11.0)
61
+ procto (~> 0.0.2)
62
+ multi_json (1.13.1)
63
+ multipart-post (2.0.0)
64
+ mutant (0.8.14)
65
+ abstract_type (~> 0.0.7)
66
+ adamantium (~> 0.2.0)
67
+ anima (~> 0.3.0)
68
+ ast (~> 2.2)
69
+ concord (~> 0.1.5)
70
+ diff-lcs (~> 1.3)
71
+ equalizer (~> 0.0.9)
72
+ ice_nine (~> 0.11.1)
73
+ memoizable (~> 0.4.2)
74
+ morpher (~> 0.2.6)
75
+ parallel (~> 1.3)
76
+ parser (>= 2.3.1.4, < 2.5)
77
+ procto (~> 0.0.2)
78
+ regexp_parser (~> 0.4.3)
79
+ unparser (~> 0.2.5)
80
+ mutant-rspec (0.8.14)
81
+ mutant (~> 0.8.14)
82
+ rspec-core (>= 3.4.0, < 3.7.0)
83
+ net-http-persistent (2.9.4)
84
+ net-http-pipeline (1.0.1)
85
+ parallel (1.12.1)
86
+ parser (2.4.0.2)
87
+ ast (~> 2.3)
88
+ powerpack (0.1.1)
89
+ procto (0.0.3)
90
+ pry (0.11.3)
91
+ coderay (~> 1.1.0)
92
+ method_source (~> 0.9.0)
93
+ pry-byebug (3.5.1)
94
+ byebug (~> 9.1)
95
+ pry (~> 0.10)
96
+ pusher-client (0.6.2)
97
+ json
98
+ websocket (~> 1.0)
99
+ rainbow (3.0.0)
100
+ rake (12.3.0)
101
+ regexp_parser (0.4.9)
102
+ rspec (3.6.0)
103
+ rspec-core (~> 3.6.0)
104
+ rspec-expectations (~> 3.6.0)
105
+ rspec-mocks (~> 3.6.0)
106
+ rspec-core (3.6.0)
107
+ rspec-support (~> 3.6.0)
108
+ rspec-expectations (3.6.0)
109
+ diff-lcs (>= 1.2.0, < 2.0)
110
+ rspec-support (~> 3.6.0)
111
+ rspec-mocks (3.6.0)
112
+ diff-lcs (>= 1.2.0, < 2.0)
113
+ rspec-support (~> 3.6.0)
114
+ rspec-support (3.6.0)
115
+ rubocop (0.52.1)
116
+ parallel (~> 1.10)
117
+ parser (>= 2.4.0.2, < 3.0)
118
+ powerpack (~> 0.1)
119
+ rainbow (>= 2.2.2, < 4.0)
120
+ ruby-progressbar (~> 1.7)
121
+ unicode-display_width (~> 1.0, >= 1.0.1)
122
+ rubocop-rspec (1.22.0)
123
+ rubocop (>= 0.52.1)
124
+ ruby-progressbar (1.9.0)
125
+ simplecov (0.15.1)
126
+ docile (~> 1.1.0)
127
+ json (>= 1.8, < 3)
128
+ simplecov-html (~> 0.10.0)
129
+ simplecov-html (0.10.2)
130
+ thor (0.20.0)
131
+ thread_safe (0.3.6)
132
+ travis (1.8.8)
133
+ backports
134
+ faraday (~> 0.9)
135
+ faraday_middleware (~> 0.9, >= 0.9.1)
136
+ gh (~> 0.13)
137
+ highline (~> 1.6)
138
+ launchy (~> 2.1)
139
+ pusher-client (~> 0.4)
140
+ typhoeus (~> 0.6, >= 0.6.8)
141
+ typhoeus (0.8.0)
142
+ ethon (>= 0.8.0)
143
+ unicode-display_width (1.3.0)
144
+ unparser (0.2.6)
145
+ abstract_type (~> 0.0.7)
146
+ adamantium (~> 0.2.0)
147
+ concord (~> 0.1.5)
148
+ diff-lcs (~> 1.3)
149
+ equalizer (~> 0.0.9)
150
+ parser (>= 2.3.1.2, < 2.5)
151
+ procto (~> 0.0.2)
152
+ websocket (1.2.5)
153
+
154
+ PLATFORMS
155
+ ruby
156
+
157
+ DEPENDENCIES
158
+ bundler
159
+ codeclimate-test-reporter
160
+ lox!
161
+ mutant
162
+ mutant-rspec
163
+ pry
164
+ pry-byebug
165
+ rake
166
+ rspec
167
+ rubocop
168
+ rubocop-rspec
169
+ simplecov
170
+ travis
171
+
172
+ BUNDLED WITH
173
+ 1.16.1
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017-2018 Richard E. Dodson
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,41 @@
1
+ # Lox
2
+
3
+ [![Build Status](https://travis-ci.org/rdodson41/ruby-lox.svg?branch=master)](https://travis-ci.org/rdodson41/ruby-lox)
4
+ [![Maintainability](https://api.codeclimate.com/v1/badges/827f986f775a6dbfc4e6/maintainability)](https://codeclimate.com/github/rdodson41/ruby-lox/maintainability)
5
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/827f986f775a6dbfc4e6/test_coverage)](https://codeclimate.com/github/rdodson41/ruby-lox/test_coverage)
6
+
7
+ The `lox` gem is an implementation of the [Lox Language](https://github.com/munificent/craftinginterpreters/wiki/Lox-implementations) which is written in Ruby.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'lox'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle install
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install lox
24
+
25
+ ## Development
26
+
27
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
28
+
29
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `lib/lox/version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rdodson41/lox. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
34
+
35
+ ## License
36
+
37
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
38
+
39
+ ## Code of Conduct
40
+
41
+ Everyone interacting in the Lox project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/rdodson41/lox/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'lox'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ require 'pry'
11
+ Pry.start
12
+
13
+ # require 'irb'
14
+ # IRB.start(__FILE__)
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env bash
2
+
3
+ set -e
4
+
5
+ bundle exec mutant --use rspec "${@}"
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+
3
+ set -e
4
+
5
+ find spec -type f -name *_spec.rb \
6
+ | xargs ag --only-matching '(?<=^RSpec\.describe )\w+(::\w+)*' \
7
+ | cut -f 3- -d : \
8
+ | xargs bundle exec mutant --use rspec
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ gem install bundler --conservative
7
+ bundle check || bundle install
8
+
9
+ # Do any other automated setup that you need to do here
data/exe/lox ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'lox/cli'
5
+
6
+ Lox::CLI.start(ARGV)
@@ -0,0 +1,11 @@
1
+ require 'lox/cli'
2
+ require 'lox/commands/write'
3
+ require 'lox/console'
4
+ require 'lox/formatter'
5
+ require 'lox/lexical_analyzer'
6
+ require 'lox/scanner'
7
+ require 'lox/version'
8
+
9
+ module Lox
10
+ # Your code goes here...
11
+ end
@@ -0,0 +1,54 @@
1
+ require 'lox/commands/write'
2
+ require 'lox/console'
3
+ require 'lox/formatter'
4
+ require 'lox/lexical_analyzer'
5
+ require 'lox/scanner'
6
+ require 'thor'
7
+
8
+ module Lox
9
+ class CLI < Thor
10
+ desc 'read', 'Read input from the console and ' \
11
+ 'write lines to standard output'
12
+ def read
13
+ Commands::Write.new(lines, STDOUT).call
14
+ end
15
+
16
+ desc 'scan', 'Scan input from the console and ' \
17
+ 'write characters to standard output'
18
+ def scan
19
+ Commands::Write.new(characters, STDOUT).call
20
+ end
21
+
22
+ desc 'lex', 'Perform lexical analysis of input from the console and ' \
23
+ 'write tokens to standard output'
24
+ def lex
25
+ Commands::Write.new(tokens, STDOUT).call
26
+ end
27
+
28
+ private
29
+
30
+ def lines
31
+ Formatter.new(console.each_line)
32
+ end
33
+
34
+ def characters
35
+ Formatter.new(scanner.each_char)
36
+ end
37
+
38
+ def tokens
39
+ Formatter.new(lexical_analyzer.each_token)
40
+ end
41
+
42
+ def console
43
+ Console.new(Readline)
44
+ end
45
+
46
+ def scanner
47
+ Scanner.new(console)
48
+ end
49
+
50
+ def lexical_analyzer
51
+ LexicalAnalyzer.new(scanner)
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,23 @@
1
+ module Lox
2
+ module Commands
3
+ class Write
4
+ attr_reader :objects
5
+ attr_reader :output
6
+
7
+ def initialize(objects, output)
8
+ @objects = objects
9
+ @output = output
10
+ end
11
+
12
+ def call
13
+ objects.each(&writer)
14
+ end
15
+
16
+ private
17
+
18
+ def writer
19
+ output.public_method(:puts)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,22 @@
1
+ module Lox
2
+ class Console
3
+ attr_reader :terminal
4
+
5
+ def initialize(terminal)
6
+ @terminal = terminal
7
+ end
8
+
9
+ def each_line
10
+ return enum_for(:each_line) unless block_given?
11
+ while (line = readline)
12
+ yield("#{line}\n")
13
+ end
14
+ end
15
+
16
+ private
17
+
18
+ def readline
19
+ terminal.readline('$ ', true)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,11 @@
1
+ module Lox
2
+ module CoreExt
3
+ module String
4
+ def match?(pattern)
5
+ !match(pattern).nil?
6
+ end
7
+ end
8
+ end
9
+ end
10
+
11
+ String.prepend(Lox::CoreExt::String)
@@ -0,0 +1,13 @@
1
+ module Lox
2
+ class Formatter
3
+ attr_reader :objects
4
+
5
+ def initialize(objects)
6
+ @objects = objects
7
+ end
8
+
9
+ def each(&block)
10
+ objects.lazy.map(&:inspect).each(&block)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,114 @@
1
+ require 'lox/core_ext/string'
2
+ require 'lox/lexical_analyzer/invalid_character'
3
+ require 'lox/lexical_analyzer/invalid_state'
4
+ require 'lox/lexical_analyzer/unterminated_string'
5
+
6
+ module Lox
7
+ class LexicalAnalyzer
8
+ attr_reader :input
9
+
10
+ def initialize(input)
11
+ @input = input
12
+ end
13
+
14
+ def each_token(&block)
15
+ return enum_for(:each_token) unless block_given?
16
+ state = :default
17
+ lexeme = nil
18
+ input.each_char do |character|
19
+ state, lexeme = method(state).call(lexeme, character, &block)
20
+ end
21
+ eof(state, lexeme, &block)
22
+ end
23
+
24
+ # rubocop:disable Metrics/CyclomaticComplexity
25
+ # rubocop:disable Metrics/MethodLength
26
+ def default(_lexeme, character)
27
+ case character
28
+ when /\s/
29
+ :default
30
+ when /[#]/
31
+ :comment
32
+ when /\d/
33
+ [:integer, character]
34
+ when /["]/
35
+ [:string, character]
36
+ when /[a-zA-Z_]/
37
+ [:identifier, character]
38
+ when /[!=<>]/
39
+ [:operator, character]
40
+ when %r{[(){},.\-+;*\/]}
41
+ yield([character])
42
+ :default
43
+ else
44
+ raise(InvalidCharacter, character)
45
+ end
46
+ end
47
+ # rubocop:enable Metrics/CyclomaticComplexity
48
+ # rubocop:enable Metrics/MethodLength
49
+
50
+ def comment(_lexeme, character)
51
+ if character.match?(/./)
52
+ :comment
53
+ else
54
+ :default
55
+ end
56
+ end
57
+
58
+ def integer(lexeme, character, &block)
59
+ if character.match?(/\d/)
60
+ [:integer, lexeme + character]
61
+ else
62
+ yield([:integer, Integer(lexeme)])
63
+ default(nil, character, &block)
64
+ end
65
+ end
66
+
67
+ def string(lexeme, character)
68
+ if character.match?(/["]/)
69
+ yield([:string, lexeme[1..-1]])
70
+ :default
71
+ else
72
+ [:string, lexeme + character]
73
+ end
74
+ end
75
+
76
+ def identifier(lexeme, character, &block)
77
+ if character.match?(/\w/)
78
+ [:identifier, lexeme + character]
79
+ else
80
+ yield([:identifier, lexeme])
81
+ default(nil, character, &block)
82
+ end
83
+ end
84
+
85
+ def operator(lexeme, character, &block)
86
+ if character.match?(/[=]/)
87
+ yield([lexeme + character])
88
+ :default
89
+ else
90
+ yield([lexeme])
91
+ default(nil, character, &block)
92
+ end
93
+ end
94
+
95
+ # rubocop:disable Metrics/MethodLength
96
+ def eof(state, lexeme)
97
+ case state
98
+ when :default
99
+ nil
100
+ when :integer
101
+ yield([:integer, Integer(lexeme)])
102
+ when :string
103
+ raise(UnterminatedString, lexeme)
104
+ when :identifier
105
+ yield([:identifier, lexeme])
106
+ when :operator
107
+ yield([lexeme])
108
+ else
109
+ raise(InvalidState, state)
110
+ end
111
+ # rubocop:enable Metrics/MethodLength
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,9 @@
1
+ module Lox
2
+ class LexicalAnalyzer
3
+ class InvalidCharacter < StandardError
4
+ def initialize(character)
5
+ super("Invalid character: #{character}")
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Lox
2
+ class LexicalAnalyzer
3
+ class InvalidState < StandardError
4
+ def initialize(state)
5
+ super("Invalid state: #{state}")
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Lox
2
+ class LexicalAnalyzer
3
+ class UnterminatedString < StandardError
4
+ def initialize(lexeme)
5
+ super("Unterminated string: #{lexeme}")
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,18 @@
1
+ module Lox
2
+ class Scanner
3
+ attr_reader :input
4
+
5
+ def initialize(input)
6
+ @input = input
7
+ end
8
+
9
+ def each_char
10
+ return enum_for(:each_char) unless block_given?
11
+ input.each_line do |line|
12
+ line.each_char do |character|
13
+ yield(character)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module Lox
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,40 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'lox/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'lox'
7
+ spec.version = Lox::VERSION
8
+ spec.authors = ['Richard E. Dodson']
9
+ spec.email = ['richard.elias.dodson@gmail.com']
10
+
11
+ spec.summary = 'An implementation of the Lox Language ' \
12
+ 'which is written in Ruby.'
13
+ spec.homepage = 'https://github.com/rdodson41/ruby-lox'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files =
17
+ `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_dependency 'thor'
25
+
26
+ spec.add_development_dependency 'bundler'
27
+ spec.add_development_dependency 'codeclimate-test-reporter'
28
+ spec.add_development_dependency 'mutant'
29
+ spec.add_development_dependency 'mutant-rspec'
30
+ spec.add_development_dependency 'pry'
31
+ spec.add_development_dependency 'pry-byebug'
32
+ spec.add_development_dependency 'rake'
33
+ spec.add_development_dependency 'rspec'
34
+ spec.add_development_dependency 'rubocop'
35
+ spec.add_development_dependency 'rubocop-rspec'
36
+ spec.add_development_dependency 'simplecov'
37
+ spec.add_development_dependency 'travis'
38
+
39
+ spec.required_ruby_version = '~> 2.2'
40
+ end
metadata ADDED
@@ -0,0 +1,258 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lox
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Richard E. Dodson
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-01-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: codeclimate-test-reporter
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mutant
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: mutant-rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry-byebug
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rake
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rspec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rubocop
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: rubocop-rspec
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: simplecov
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ - !ruby/object:Gem::Dependency
182
+ name: travis
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - ">="
193
+ - !ruby/object:Gem::Version
194
+ version: '0'
195
+ description:
196
+ email:
197
+ - richard.elias.dodson@gmail.com
198
+ executables:
199
+ - lox
200
+ extensions: []
201
+ extra_rdoc_files: []
202
+ files:
203
+ - ".codeclimate.yml"
204
+ - ".gitignore"
205
+ - ".rspec"
206
+ - ".rubocop.yml"
207
+ - ".ruby-version"
208
+ - ".simplecov"
209
+ - ".travis.yml"
210
+ - CODE_OF_CONDUCT.md
211
+ - Gemfile
212
+ - Gemfile.lock
213
+ - LICENSE.txt
214
+ - README.md
215
+ - Rakefile
216
+ - bin/console
217
+ - bin/mutant
218
+ - bin/mutant-all
219
+ - bin/setup
220
+ - exe/lox
221
+ - lib/lox.rb
222
+ - lib/lox/cli.rb
223
+ - lib/lox/commands/write.rb
224
+ - lib/lox/console.rb
225
+ - lib/lox/core_ext/string.rb
226
+ - lib/lox/formatter.rb
227
+ - lib/lox/lexical_analyzer.rb
228
+ - lib/lox/lexical_analyzer/invalid_character.rb
229
+ - lib/lox/lexical_analyzer/invalid_state.rb
230
+ - lib/lox/lexical_analyzer/unterminated_string.rb
231
+ - lib/lox/scanner.rb
232
+ - lib/lox/version.rb
233
+ - lox.gemspec
234
+ homepage: https://github.com/rdodson41/ruby-lox
235
+ licenses:
236
+ - MIT
237
+ metadata: {}
238
+ post_install_message:
239
+ rdoc_options: []
240
+ require_paths:
241
+ - lib
242
+ required_ruby_version: !ruby/object:Gem::Requirement
243
+ requirements:
244
+ - - "~>"
245
+ - !ruby/object:Gem::Version
246
+ version: '2.2'
247
+ required_rubygems_version: !ruby/object:Gem::Requirement
248
+ requirements:
249
+ - - ">="
250
+ - !ruby/object:Gem::Version
251
+ version: '0'
252
+ requirements: []
253
+ rubyforge_project:
254
+ rubygems_version: 2.7.3
255
+ signing_key:
256
+ specification_version: 4
257
+ summary: An implementation of the Lox Language which is written in Ruby.
258
+ test_files: []