rFlex 0.1.2

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/rflex.rb +145 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0d1411f324f26e88afc9c45dc3ceeb35baa089f6
4
+ data.tar.gz: 24a7faa87472ac481a14475142845b3cee93ff1d
5
+ SHA512:
6
+ metadata.gz: b721b4b5e32188a778168fd2006754e066832e7b9ba8ef300c179c0e7336458abbb31af91c9cf5144356f5d0ff1643932e1d83482828852efa6d0949d914c8ec
7
+ data.tar.gz: d140e1023b1447d514a811a1f0d59e444f321fa4dc3b20d327b0d5de86e6cd23b63b9ca241877bd61dfdf0ab02100c84ce164e40aac80f2bd0af78389b6b9c3f
data/lib/rflex.rb ADDED
@@ -0,0 +1,145 @@
1
+ #! /bin/env ruby
2
+ #
3
+ ############################################################
4
+ # Author: Alice "Duchess" Archer
5
+ # Copyright (c) 2016 under the MIT License
6
+ # see COPYING.md for full copyright
7
+ # Name: rFlex
8
+ # Description: lexical analysis library
9
+ ############################################################
10
+
11
+ class TokenLexer
12
+ def initialize type, regexp
13
+ @type = type
14
+ @regexp = regexp
15
+ @blockType = nil
16
+ @block = nil
17
+ end
18
+
19
+ def do type, &block
20
+ @block = block
21
+ @blockType = type
22
+ end
23
+
24
+ def lex input
25
+
26
+ if token = input.slice!(@regexp)
27
+ if @block != nil
28
+ analyze(token, @regexp, @type)
29
+ end
30
+ return true
31
+ end
32
+
33
+ return false
34
+ end
35
+
36
+ def analyze token, regex, type
37
+ @block.call(token, regex, type)
38
+ end
39
+
40
+ def type
41
+ return @type
42
+ end
43
+
44
+ def regexp
45
+ return @regexp
46
+ end
47
+
48
+ def block
49
+ return @block
50
+ end
51
+ end
52
+
53
+ class LexicalAnalyzer
54
+ def initialize file, config
55
+ @types = {}
56
+ @typeList = []
57
+ @file = file
58
+ @streamTokens = []
59
+ @lexers = []
60
+ @typeList = []
61
+ configure(config)
62
+ end
63
+
64
+ def configure config
65
+ typeList = File.readlines(config)
66
+
67
+ 0.upto(typeList.length.to_i - 1) do |i|
68
+ typeList[i].chomp!
69
+ end
70
+
71
+ typeList.each do |type|
72
+
73
+ at_Reg = false
74
+
75
+ cur_Type = ""
76
+ cur_Reg = ""
77
+
78
+ index = 0
79
+
80
+ 0.upto(type.length - 1) do |i|
81
+ if type[i] == "{"
82
+ index = i
83
+ break
84
+ else
85
+ cur_Type.concat(type[i])
86
+ end
87
+ end
88
+
89
+ cur_Reg = type[(index+1)..-2]
90
+
91
+ cur_Type.delete! " "
92
+
93
+ @types.store(cur_Type, Regexp.new(cur_Reg))
94
+ cur_Reg = Regexp.new(cur_Reg)
95
+ @lexers.push(TokenLexer.new(cur_Type, cur_Reg))
96
+ @typeList.push(cur_Type)
97
+ end
98
+ end
99
+
100
+ def lex
101
+ lines = File.readlines(@file)
102
+ input = ""
103
+ lines.each do |line|
104
+ input.concat("#{line}")
105
+ end
106
+
107
+ input = input[0..-2].to_s
108
+
109
+ len = @lexers.length - 1
110
+ j = 0
111
+
112
+ matched = false
113
+
114
+ until input.length == 0
115
+ 0.upto(len) do |i|
116
+
117
+ if @lexers[i].lex(input)
118
+ matched = true
119
+ else
120
+ end
121
+ j = i
122
+ end
123
+
124
+ # if we tried all options
125
+ # and
126
+ # we didn't match
127
+ # and
128
+ # there is remaining input
129
+ if j == len and matched == false and input.length != 0
130
+ puts "\n\nUnconsumed Input:\n\n#{input}\n\n"
131
+ abort
132
+ end
133
+
134
+ matched = false
135
+ end
136
+ end
137
+
138
+ def lexers
139
+ return @lexers
140
+ end
141
+
142
+ def types
143
+ return @typeList
144
+ end
145
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rFlex
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Alice "Duchess" Archer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-02-11 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: ruby lexical analysis library
14
+ email: iarcher@pdx.edu
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/rflex.rb
20
+ homepage: https://github.com/The-Duchess/rFlex
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.2.3
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: rflex
44
+ test_files: []