kansuji_to_number 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 721bccbabd674c767bbe18621c735bffeb78e25d4136fd0a7dab17b24de91bef
4
+ data.tar.gz: c2c631030db26281e58f726ba4a8b22a8870b51e85c8b8481c82e8d214e008de
5
+ SHA512:
6
+ metadata.gz: 263a305b60bbce61be3dd0d96e44ecc27a1fc9d794b8bb3b8d7441cf082bcc6fcd4ef3924afc8550dbd6c25ffba930b6554e01c2dfae671ce210ec2a8d782fea
7
+ data.tar.gz: 334d00f01571ed95ede725ac1dc52f93dc34ecfc7318776f97ca365c21fb365b2005d91c49509836264ecebf4f9245e48b7e19b7fbf63e212e2de8ba921fa806
@@ -0,0 +1,18 @@
1
+ # [Choice] Ruby version (use -bullseye variants on local arm64/Apple Silicon): 3, 3.0, 2, 2.7, 2.6, 3-bullseye, 3.0-bullseye, 2-bullseye, 2.7-bullseye, 2.6-bullseye, 3-buster, 3.0-buster, 2-buster, 2.7-buster, 2.6-buster
2
+ ARG VARIANT=2-bullseye
3
+ FROM mcr.microsoft.com/vscode/devcontainers/ruby:0-${VARIANT}
4
+
5
+ # [Choice] Node.js version: none, lts/*, 16, 14, 12, 10
6
+ ARG NODE_VERSION="none"
7
+ RUN if [ "${NODE_VERSION}" != "none" ]; then su vscode -c "umask 0002 && . /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi
8
+
9
+ # [Optional] Uncomment this section to install additional OS packages.
10
+ # RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
11
+ # && apt-get -y install --no-install-recommends <your-package-list-here>
12
+
13
+ # [Optional] Uncomment this line to install additional gems.
14
+ # RUN gem install <your-gem-names-here>
15
+ RUN git config --global pull.rebase false
16
+
17
+ # [Optional] Uncomment this line to install global node packages.
18
+ # RUN su vscode -c "source /usr/local/share/nvm/nvm.sh && npm install -g <your-package-here>" 2>&1
@@ -0,0 +1,43 @@
1
+ # [Choice] Ruby version (use -bullseye variants on local arm64/Apple Silicon): 3, 3.0, 2, 2.7, 2.6, 3-bullseye, 3.0-bullseye, 2-bullseye, 2.7-bullseye, 2.6-bullseye, 3-buster, 3.0-buster, 2-buster, 2.7-buster, 2.6-buster
2
+ ARG VARIANT=2-bullseye
3
+ FROM ruby:${VARIANT}
4
+
5
+ # Copy library scripts to execute
6
+ COPY library-scripts/*.sh library-scripts/*.env /tmp/library-scripts/
7
+
8
+ # [Option] Install zsh
9
+ ARG INSTALL_ZSH="true"
10
+ # [Option] Upgrade OS packages to their latest versions
11
+ ARG UPGRADE_PACKAGES="true"
12
+ # Install needed packages and setup non-root user. Use a separate RUN statement to add your own dependencies.
13
+ ARG USERNAME=vscode
14
+ ARG USER_UID=1000
15
+ ARG USER_GID=$USER_UID
16
+ RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
17
+ # Remove imagemagick due to https://security-tracker.debian.org/tracker/CVE-2019-10131
18
+ && apt-get purge -y imagemagick imagemagick-6-common \
19
+ # Install common packages, non-root user, rvm, core build tools
20
+ && bash /tmp/library-scripts/common-debian.sh "${INSTALL_ZSH}" "${USERNAME}" "${USER_UID}" "${USER_GID}" "${UPGRADE_PACKAGES}" "true" "true" \
21
+ && bash /tmp/library-scripts/ruby-debian.sh "none" "${USERNAME}" "true" "true" \
22
+ && apt-get autoremove -y && apt-get clean -y && rm -rf /var/lib/apt/lists/*
23
+
24
+ # [Choice] Node.js version: none, lts/*, 16, 14, 12, 10
25
+ ARG NODE_VERSION="none"
26
+ ENV NVM_DIR=/usr/local/share/nvm
27
+ ENV NVM_SYMLINK_CURRENT=true \
28
+ PATH=${NVM_DIR}/current/bin:${PATH}
29
+ RUN bash /tmp/library-scripts/node-debian.sh "${NVM_DIR}" "${NODE_VERSION}" "${USERNAME}" \
30
+ && apt-get clean -y && rm -rf /var/lib/apt/lists/*
31
+
32
+ # Remove library scripts for final image
33
+ RUN rm -rf /tmp/library-scripts
34
+
35
+ # [Optional] Uncomment this section to install additional OS packages.
36
+ # RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
37
+ # && apt-get -y install --no-install-recommends <your-package-list-here>
38
+
39
+ # [Optional] Uncomment this line to install additional gems.
40
+ # RUN gem install <your-gem-names-here>
41
+
42
+ # [Optional] Uncomment this line to install global node packages.
43
+ # RUN su vscode -c "source /usr/local/share/nvm/nvm.sh && npm install -g <your-package-here>" 2>&1
@@ -0,0 +1,32 @@
1
+ // For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
2
+ // https://github.com/microsoft/vscode-dev-containers/tree/v0.203.0/containers/ruby
3
+ {
4
+ "name": "Ruby",
5
+ "runArgs": [
6
+ "--init"
7
+ ],
8
+ "build": {
9
+ "dockerfile": "Dockerfile",
10
+ "args": {
11
+ // Update 'VARIANT' to pick a Ruby version: 3, 3.0, 2, 2.7, 2.6
12
+ // Append -bullseye or -buster to pin to an OS version.
13
+ // Use -bullseye variants on local on arm64/Apple Silicon.
14
+ "VARIANT": "3-bullseye",
15
+ // Options
16
+ "NODE_VERSION": "lts/*"
17
+ }
18
+ },
19
+ // Set *default* container specific settings.json values on container create.
20
+ "settings": {},
21
+ // Add the IDs of extensions you want installed when the container is created.
22
+ "extensions": [
23
+ "rebornix.Ruby"
24
+ ],
25
+ // Use 'forwardPorts' to make a list of ports inside the container available locally.
26
+ // "forwardPorts": [],
27
+ // Use 'postCreateCommand' to run commands after the container is created.
28
+ // "postCreateCommand": "ruby --version",
29
+ // Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
30
+ "remoteUser": "vscode",
31
+ "postCreateCommand": "bundle",
32
+ }
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.4
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at xiidec@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in kansuji_to_number.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest", "~> 5.0"
11
+
12
+ gem "rubocop", "~> 1.7"
13
+
14
+ gem 'parslet', '~> 2.0'
15
+
data/Gemfile.lock ADDED
@@ -0,0 +1,44 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ kansuji_to_number (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ minitest (5.14.4)
11
+ parallel (1.21.0)
12
+ parser (3.0.3.1)
13
+ ast (~> 2.4.1)
14
+ parslet (2.0.0)
15
+ rainbow (3.0.0)
16
+ rake (13.0.6)
17
+ regexp_parser (2.1.1)
18
+ rexml (3.2.5)
19
+ rubocop (1.23.0)
20
+ parallel (~> 1.10)
21
+ parser (>= 3.0.0.0)
22
+ rainbow (>= 2.2.2, < 4.0)
23
+ regexp_parser (>= 1.8, < 3.0)
24
+ rexml
25
+ rubocop-ast (>= 1.12.0, < 2.0)
26
+ ruby-progressbar (~> 1.7)
27
+ unicode-display_width (>= 1.4.0, < 3.0)
28
+ rubocop-ast (1.13.0)
29
+ parser (>= 3.0.1.1)
30
+ ruby-progressbar (1.11.0)
31
+ unicode-display_width (2.1.0)
32
+
33
+ PLATFORMS
34
+ x86_64-linux
35
+
36
+ DEPENDENCIES
37
+ kansuji_to_number!
38
+ minitest (~> 5.0)
39
+ parslet (~> 2.0)
40
+ rake (~> 13.0)
41
+ rubocop (~> 1.7)
42
+
43
+ BUNDLED WITH
44
+ 2.2.26
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 kurehajime
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.
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # KansujiToNumber
2
+
3
+ Converts Kansuji(漢数字) to numbers.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'kansuji_to_number'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install kansuji_to_number
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require 'kansuji_to_number'
25
+ result = KansujiToNumber.trans("一兆二億三万四千五百六十七")
26
+ p result # => 1000200034567
27
+ ```
28
+
29
+ ## Development
30
+
31
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
32
+
33
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
34
+
35
+ ## Contributing
36
+
37
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/kansuji_to_number. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/kansuji_to_number/blob/master/CODE_OF_CONDUCT.md).
38
+
39
+ ## License
40
+
41
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
42
+
43
+ ## Code of Conduct
44
+
45
+ Everyone interacting in the KansujiToNumber project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/kansuji_to_number/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ require "rubocop/rake_task"
13
+
14
+ RuboCop::RakeTask.new
15
+
16
+ task default: %i[test rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "kansuji_to_number"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "kansuji_to_number"
4
+ p KansujiToNumber.trans(ARGV[0]) if ARGV[0]
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/kansuji_to_number/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "kansuji_to_number"
7
+ spec.version = KansujiToNumber::VERSION
8
+ spec.authors = ["kurehajime"]
9
+ spec.email = ["xiidec@gmail.com"]
10
+
11
+ spec.summary = "Converts Kansuji(漢数字) to numbers."
12
+ spec.description = "Converts Kansuji(漢数字) to numbers."
13
+ spec.homepage = "https://github.com/kurehajime/kansuji_to_number"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.4.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/kurehajime/kansuji_to_number"
19
+ spec.metadata["changelog_uri"] = "https://github.com/kurehajime/kansuji_to_number"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ # Uncomment to register a new dependency of your gem
31
+ # spec.add_dependency "example-gem", "~> 1.0"
32
+
33
+ # For more information and examples about making a new gem, checkout our
34
+ # guide at: https://bundler.io/guides/creating_gem.html
35
+ end
@@ -0,0 +1,29 @@
1
+ module KansujiToNumber
2
+ PureExpressionNode = Struct.new(:args) do
3
+ def eval
4
+ figure = 1
5
+ sum = args.reverse.inject(0) {|result, item|
6
+ result = result + item * figure
7
+ figure *= 10
8
+ result
9
+ }
10
+ sum.to_i
11
+ end
12
+ end
13
+
14
+ Layer1ExpressionNode = Struct.new(:left, :unit, :right) do
15
+ def eval
16
+ l = left ? left.eval : 1
17
+ r = right ? right.eval : 0
18
+ l * unit + r
19
+ end
20
+ end
21
+
22
+ Layer2ExpressionNode = Struct.new(:left, :unit, :right) do
23
+ def eval
24
+ l = left ? left.eval : 1
25
+ r = right ? right.eval : 0
26
+ l * unit + r
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,74 @@
1
+ require 'parslet'
2
+ module KansujiToNumber
3
+ class KansujiParser < Parslet::Parser
4
+
5
+ # non number
6
+ rule(:space) { match('\s').repeat(1) }
7
+ rule(:space?) { space.maybe }
8
+
9
+ # pure number
10
+ rule(:int) { match('[0-9]').as(:int) >> space? }
11
+ rule(:wide_int) { match('[1234567890]').as(:wide_int) >> space? }
12
+ rule(:lit_0) { (str('零') | str('〇') ).as(:lit_0) >> space? }
13
+ rule(:lit_1) { str('一').as(:lit_1) >> space? }
14
+ rule(:lit_2) { str('二').as(:lit_2) >> space? }
15
+ rule(:lit_3) { str('三').as(:lit_3) >> space? }
16
+ rule(:lit_4) { str('四').as(:lit_4) >> space? }
17
+ rule(:lit_5) { str('五').as(:lit_5) >> space? }
18
+ rule(:lit_6) { str('六').as(:lit_6) >> space? }
19
+ rule(:lit_7) { str('七').as(:lit_7) >> space? }
20
+ rule(:lit_8) { str('八').as(:lit_8) >> space? }
21
+ rule(:lit_9) { str('九').as(:lit_9) >> space? }
22
+
23
+ # layer 1 unit
24
+ rule(:lit_10) { (str('十')).as(:lit_10) >> space? }
25
+ rule(:lit_100) { (str('百')).as(:lit_100) >> space? }
26
+ rule(:lit_1000) { (str('千')).as(:lit_1000) >> space? }
27
+
28
+ # layer 2 unit
29
+ rule(:lit_10000) { (str('万')).as(:lit_10000) >> space? }
30
+ rule(:lit_100000000) { (str('億')).as(:lit_100000000) >> space? }
31
+ rule(:lit_1000000000000){ (str('兆')).as(:lit_1000000000000) >> space? }
32
+
33
+ # group
34
+ rule(:pure_number){(
35
+ lit_0|
36
+ lit_1|
37
+ lit_2|
38
+ lit_3|
39
+ lit_4|
40
+ lit_5|
41
+ lit_6|
42
+ lit_7|
43
+ lit_8|
44
+ lit_9|
45
+ int|
46
+ wide_int).as(:pure_number) }
47
+
48
+ rule(:layer_1_unit){(
49
+ lit_10|
50
+ lit_100|
51
+ lit_1000).as(:layer_1_unit) }
52
+
53
+ rule(:layer_2_unit){(
54
+ lit_10000|
55
+ lit_100000000|
56
+ lit_1000000000000).as(:layer_2_unit) }
57
+
58
+ # expression
59
+ rule(:pure_expression){pure_number.repeat(1).as(:pure_expression) }
60
+
61
+ rule(:layer_1_expression){
62
+ (pure_expression.maybe.as(:left) >> layer_1_unit.as(:unit) >> layer_1_expression.maybe.as(:right)).as(:layer_1_expression) |
63
+ pure_expression
64
+ }
65
+
66
+ rule(:layer_2_expression){
67
+ (layer_1_expression.as(:left) >> layer_2_unit.as(:unit) >> layer_2_expression.maybe.as(:right)).as(:layer_2_expression) |
68
+ layer_1_expression
69
+ }
70
+
71
+ # root
72
+ root :layer_2_expression
73
+ end
74
+ end
@@ -0,0 +1,14 @@
1
+ require 'parslet'
2
+ require_relative 'kansuji_parser'
3
+ require_relative 'kansuji_trans'
4
+ module KansujiToNumber
5
+ def self.parse(str)
6
+ KansujiToNumber::KansujiParser.new.parse(str.strip)
7
+ end
8
+
9
+ def self.trans(str)
10
+ parsed = KansujiToNumber::KansujiParser.new.parse(str.strip)
11
+ transed = KansujiToNumber::KansujiTrans.new.apply(parsed)
12
+ transed.eval
13
+ end
14
+ end
@@ -0,0 +1,40 @@
1
+ require 'parslet'
2
+ require_relative 'kansuji_nodes'
3
+ module KansujiToNumber
4
+ class KansujiTrans < Parslet::Transform
5
+ rule(lit_0: simple(:x)) { 0 }
6
+ rule(lit_1: simple(:x)) { 1 }
7
+ rule(lit_2: simple(:x)) { 2 }
8
+ rule(lit_3: simple(:x)) { 3 }
9
+ rule(lit_4: simple(:x)) { 4 }
10
+ rule(lit_5: simple(:x)) { 5 }
11
+ rule(lit_6: simple(:x)) { 6 }
12
+ rule(lit_7: simple(:x)) { 7 }
13
+ rule(lit_8: simple(:x)) { 8 }
14
+ rule(lit_9: simple(:x)) { 9 }
15
+ rule(int: simple(:x)) { x.to_i }
16
+ rule(wide_int: simple(:x)) { x.to_s.tr("0-9", "0-9").to_i }
17
+ rule(lit_10: simple(:x)) { 10 }
18
+ rule(lit_100: simple(:x)) { 100 }
19
+ rule(lit_1000: simple(:x)) { 1000 }
20
+ rule(lit_10000: simple(:x)) { 10000 }
21
+ rule(lit_100000000: simple(:x)) { 100000000 }
22
+ rule(lit_1000000000000: simple(:x)) { 1000000000000 }
23
+
24
+ rule(pure_number: simple(:x)) { x }
25
+ rule(layer_1_unit: simple(:x)) { x }
26
+ rule(layer_2_unit: simple(:x)) { x }
27
+
28
+ rule(pure_expression: sequence(:args)) {
29
+ PureExpressionNode.new(args)
30
+ }
31
+
32
+ rule(layer_1_expression: subtree(:tree)) {
33
+ Layer1ExpressionNode.new(tree[:left],tree[:unit],tree[:right])
34
+ }
35
+
36
+ rule(layer_2_expression: subtree(:tree)) {
37
+ Layer2ExpressionNode.new(tree[:left],tree[:unit],tree[:right])
38
+ }
39
+ end
40
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KansujiToNumber
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+ require_relative "kansuji_to_number/kansuji_nodes"
3
+ require_relative "kansuji_to_number/kansuji_parser"
4
+ require_relative "kansuji_to_number/kansuji_to_number"
5
+ require_relative "kansuji_to_number/kansuji_trans"
6
+ require_relative "kansuji_to_number/version"
7
+ require_relative "kansuji_to_number/version"
8
+ require_relative "kansuji_to_number/version"
9
+ require_relative "kansuji_to_number/version"
10
+
11
+ module KansujiToNumber
12
+ class Error < StandardError; end
13
+ # Your code goes here...
14
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kansuji_to_number
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - kurehajime
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-11-30 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Converts Kansuji(漢数字) to numbers.
14
+ email:
15
+ - xiidec@gmail.com
16
+ executables:
17
+ - kansuji_to_number
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".devcontainer/Dockerfile"
22
+ - ".devcontainer/base.Dockerfile"
23
+ - ".devcontainer/devcontainer.json"
24
+ - ".gitignore"
25
+ - ".rubocop.yml"
26
+ - CODE_OF_CONDUCT.md
27
+ - Gemfile
28
+ - Gemfile.lock
29
+ - LICENSE.txt
30
+ - README.md
31
+ - Rakefile
32
+ - bin/console
33
+ - bin/setup
34
+ - exe/kansuji_to_number
35
+ - kansuji_to_number.gemspec
36
+ - lib/kansuji_to_number.rb
37
+ - lib/kansuji_to_number/kansuji_nodes.rb
38
+ - lib/kansuji_to_number/kansuji_parser.rb
39
+ - lib/kansuji_to_number/kansuji_to_number.rb
40
+ - lib/kansuji_to_number/kansuji_trans.rb
41
+ - lib/kansuji_to_number/version.rb
42
+ homepage: https://github.com/kurehajime/kansuji_to_number
43
+ licenses:
44
+ - MIT
45
+ metadata:
46
+ homepage_uri: https://github.com/kurehajime/kansuji_to_number
47
+ source_code_uri: https://github.com/kurehajime/kansuji_to_number
48
+ changelog_uri: https://github.com/kurehajime/kansuji_to_number
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: 2.4.0
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubygems_version: 3.2.22
65
+ signing_key:
66
+ specification_version: 4
67
+ summary: Converts Kansuji(漢数字) to numbers.
68
+ test_files: []