lexical_analyzer 0.3.0 → 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 17cf068290c697c20216d7e8a171392caa14dc9f
4
- data.tar.gz: 44cc961d916c2b03e226138ff11c0b637f54e52b
2
+ SHA256:
3
+ metadata.gz: 183cf3a641ff2b13b77349e4f57ba380f48d4ff41590186b5759ed6e4cea1a0a
4
+ data.tar.gz: cc38ee450153021a29fe4dd8229f5f62ad1e30b2116359d0117a98f05173e77e
5
5
  SHA512:
6
- metadata.gz: 80a7590b7abd987fc22cdf2be1b547a79ec2b04d4ba39d9961eb71544a03ef1604935e01b66d12da0198cb8f6e5c3e7215cb74d513b018e8ebd90307449eab82
7
- data.tar.gz: 8f0483b6d822154daf2757ebeb0fb28a35ab38a0b9e42e8da10615d5d075f7f20c78b633487b10d405792e532fd3a6546ed6568344ba29bc77939c005b8fb912
6
+ metadata.gz: bfeb055736ade9ddcc4ec429f072fb51e8f42684beb22b6a4af21230f89bc6b1d70c7be6750d3c76c9d6ac02b20ad6192376712874765700e7de831ea7ac5082
7
+ data.tar.gz: d82439f9985d528010ebf4c3dad7aec350ed6b660e6bb840a4fa733861f0cf906d05204a6fcb3310176ca24836eb9348e85eee7d06923b43bc68f7176ab21499
data/README.md CHANGED
@@ -1,10 +1,21 @@
1
1
  # LexicalAnalyzer
2
2
 
3
- The lexical analyzer is a component of the Ruby Compiler Toolkit Project that
4
- scans an input text against an array of rules and generating the lexical
5
- tokens that it detects. It is normally used in conjunction with a parse queue
6
- object which handles queuing of tokens and back tracking of the compile process
7
- when needed.
3
+ The lexical analyzer is a component of the Ruby Compiler Toolkit Project (rctp)
4
+ that scans an input text against an array of rules and generating the lexical
5
+ tokens that it detects. This process is shown below:
6
+
7
+ ![The Lexical Process](./images/lexical_process.png)
8
+
9
+ In general, each time the lexical_analyzer receives the "get" message, it tries
10
+ to extract another token from the source text. As such, the lexical analyzer
11
+ gem component is the first stage of the compilation process for a compiler
12
+ built using the rctp. With its array of lexical rules it provides the language
13
+ tokens needed to operate the compiler's parser.
14
+
15
+ The lexical analyzer is normally used in conjunction with a parse queue object
16
+ which handles queuing of tokens and back tracking of the compile process when
17
+ needed. In the rcpt this is done by the gem
18
+ [parse_queue](https://github.com/PeterCamilleri/parse_queue).
8
19
 
9
20
  ## Installation
10
21
 
@@ -58,27 +69,32 @@ regular expression, and an optional action.
58
69
  # Rule with default block returns [:equality, "=="] on a match.
59
70
  LexicalRule.new(:equality, /\A==/)
60
71
 
72
+ # Rule with block equivalent to the default.
73
+ LexicalRule.new(:lparen, /\A\(/) {|value| [symbol, value]}
74
+
61
75
  # Rule with an ignore block, ignores matches.
62
76
  LexicalRule.new(:spaces, /\A\s+/) {|_value| false }
63
77
 
64
78
  # Rule with an integer block returns [:integer, an_integer] on a match.
65
- LexicalRule.new(:integer, /\A\d+/) {|value| [@symbol, value.to_i] }
79
+ LexicalRule.new(:integer, /\A\d+/) {|value| [symbol, value.to_i] }
66
80
 
67
81
  # Rule with a block that expands of to a sub-rule. Returns the value of the
68
82
  # lexical analyzer in the captured variable ka.
69
- LexicalRule.new(:identifier, /\A[a-zA-Z_]\w*(?=\W|$|\z)/) {|value|
70
- ka.renew(text: value).get
71
- }
83
+ LexicalRule.new(:identifier, /\A[a-zA-Z_]\w*(?=\W|$|\z)/) {|value| ka.renew(text: value).get}
72
84
  ```
73
85
 
74
86
  Notes:
75
87
 
76
88
  * The regular expression must begin with a \A clause to ensure correct
77
89
  operation of the analyzer.
90
+ * An exception to the above is the use of the expression /.+/ at the end of the
91
+ rule list as a sort of lexical "else" catch-all clause.
78
92
  * The order of rules is important. For example, if there are two rules
79
93
  looking for "==" and "=" respectively, if the "=" is ahead of the "==" rule
80
94
  in the array the "==" rule will never trigger and the analysis will be
81
95
  incorrect.
96
+ * The method LexicalRule#symbol is a read accessor for the symbol property of
97
+ the lexical rule.
82
98
 
83
99
  #### Tokens
84
100
 
Binary file
@@ -19,8 +19,8 @@ Gem::Specification.new do |spec|
19
19
 
20
20
  spec.required_ruby_version = '>=2.3.0'
21
21
 
22
- spec.add_development_dependency "bundler", "~> 1.15"
23
- spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency "bundler", ">= 2.1.0"
23
+ spec.add_development_dependency "rake", ">= 12.3.3"
24
24
  spec.add_development_dependency "minitest", "~> 5.0"
25
25
  spec.add_development_dependency 'reek', "~> 5.0.2"
26
26
  end
@@ -6,8 +6,8 @@ require_relative 'lexical_analyzer/version'
6
6
 
7
7
  # The RCTP class for lexical analysis.
8
8
  class LexicalAnalyzer
9
- attr_reader :text # Access the text in the analyzer.
10
- attr_reader :rules # Access the array of lexical rules.
9
+ attr_reader :text # Access the text in the analyzer.
10
+ attr_reader :rules # Access the array of lexical rules.
11
11
 
12
12
  # Set things up.
13
13
  def initialize(text: "", rules: [])
@@ -23,8 +23,8 @@ class LexicalAnalyzer
23
23
  end
24
24
 
25
25
  # Get the next lexical token
26
- def get(extra=[])
27
- (rules + extra).each do |rule|
26
+ def get(extra=nil)
27
+ (extra ? rules + extra : rules).each do |rule|
28
28
  if match_data = rule.match(text)
29
29
  @text = match_data.post_match
30
30
  return rule.call(match_data.to_s) || get
@@ -1,24 +1,26 @@
1
- # The Ruby Compiler Toolkit Project - Lexical Rule
2
- # A rule for lexical analysis.
3
-
4
- class LexicalRule
5
-
6
- # Create a lexical rule.
7
- def initialize(symbol, regex, &action)
8
- @symbol = symbol
9
- @regex = regex
10
-
11
- define_singleton_method(:call, &action) if block_given?
12
- end
13
-
14
- # Does this rule match?
15
- def match(text)
16
- text.match(@regex)
17
- end
18
-
19
- # The default rule action.
20
- def call(value)
21
- [@symbol, value]
22
- end
23
-
24
- end
1
+ # The Ruby Compiler Toolkit Project - Lexical Rule
2
+ # A rule for lexical analysis.
3
+
4
+ # The RCTP class for lexical rules.
5
+ class LexicalRule
6
+ attr_reader :symbol # Read access to the rule's symbol.
7
+ attr_reader :regex # Read access to the rule's regex.
8
+
9
+ # Create a lexical rule.
10
+ def initialize(symbol, regex, &action)
11
+ @symbol = symbol
12
+ @regex = regex
13
+
14
+ define_singleton_method(:call, &action) if block_given?
15
+ end
16
+
17
+ # Does this rule match?
18
+ def match(text)
19
+ text.match(@regex)
20
+ end
21
+
22
+ # The default rule action.
23
+ def call(value)
24
+ [symbol, value]
25
+ end
26
+ end
@@ -1,3 +1,3 @@
1
1
  class LexicalAnalyzer
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.5".freeze
3
3
  end
metadata CHANGED
@@ -1,43 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lexical_analyzer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - PeterCamilleri
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-03 00:00:00.000000000 Z
11
+ date: 2021-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.15'
19
+ version: 2.1.0
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.15'
26
+ version: 2.1.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: 12.3.3
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: 12.3.3
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: minitest
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -78,6 +78,7 @@ files:
78
78
  - Gemfile
79
79
  - LICENSE.txt
80
80
  - README.md
81
+ - images/lexical_process.png
81
82
  - lexical_analyzer.gemspec
82
83
  - lib/lexical_analyzer.rb
83
84
  - lib/lexical_analyzer/lexical_rule.rb
@@ -103,8 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
103
104
  - !ruby/object:Gem::Version
104
105
  version: '0'
105
106
  requirements: []
106
- rubyforge_project:
107
- rubygems_version: 2.5.2
107
+ rubygems_version: 3.2.17
108
108
  signing_key:
109
109
  specification_version: 4
110
110
  summary: The RCTP lexical analyser.