posxml_parser 1.3.1 → 2.0.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,5 @@
1
+ module PosxmlCompiler
2
+ class VariableLimitError < PosxmlCompilerError
3
+ end
4
+ end
5
+
@@ -0,0 +1,5 @@
1
+ module PosxmlCompiler
2
+ class VariableNameError < PosxmlCompilerError
3
+ end
4
+ end
5
+
@@ -0,0 +1,5 @@
1
+ module PosxmlCompiler
2
+ class VariableStringLimitError < PosxmlCompilerError
3
+ end
4
+ end
5
+
@@ -0,0 +1,5 @@
1
+ module PosxmlCompiler
2
+ class VariableTypeError < PosxmlCompilerError
3
+ end
4
+ end
5
+
@@ -0,0 +1,5 @@
1
+ module PosxmlCompiler
2
+ class VariableUndefinedError < PosxmlCompilerError
3
+ end
4
+ end
5
+
@@ -0,0 +1,161 @@
1
+ module PosxmlCompiler
2
+ class XsdParser
3
+ DELIMITER_END_INSTRUCTION = "\r"
4
+ DELIMITER_END_PARAMETER = "\n"
5
+
6
+ attr_accessor :text, :instructions
7
+
8
+ def initialize(text)
9
+ @text = text
10
+ index_start = text.index("<xs:group name=\"comandos\">")
11
+ index_end = text.index("<\/xs:group>")
12
+ groups = text[index_start..index_end] # select only group text
13
+
14
+ complex = text[index_end..-1] # select only complex text
15
+
16
+ commands = parse_bytecodes(groups)
17
+ @instructions = parse_parameters(commands, complex)
18
+ # => {"if" => {:bytecode => "I", :order => ["variable", "operator", "value"], :parameters => {"variable" => :string_or_integer, "operator" => :string, "value" => :string_or_integer}}
19
+ end
20
+
21
+ def valid?
22
+ !! @instructions
23
+ end
24
+
25
+ def parameter_type(instruction, parameter)
26
+ self.instructions[instruction][:parameters][parameter.to_s]
27
+ end
28
+
29
+ # Receive:
30
+ # ("if", {"variable" => {:original => "$(sKeyTouchScreen)", :value => "$(1)", :type => :string}, "operator" => {:original => "equalto", :value => "equalto", :type => :string}, "value" => {:original => "KEY_CLEAR", :value => "KEY_CLEAR", :type => :string})
31
+ # Return:
32
+ # "I$(1)\nequalto\nKEY_CLEAR"
33
+ def to_bytecode(name, parameters)
34
+ instruction = self.find(name) # => {:bytecode => "I", :order => ["variable", "operator", "value"], :parameters => {"variable" => :string_or_integer, "operator" => :string, "value" => :string_or_integer}
35
+ raise InstructionNotFoundError.new("Instruction #{name.inspect} not found") unless instruction
36
+
37
+ line = instruction[:order].collect { |param| parameters[param][:value] || parameters[param][:value]}
38
+
39
+ if line.empty?
40
+ instruction[:bytecode].chr + DELIMITER_END_INSTRUCTION
41
+ else
42
+ instruction[:bytecode].chr + line.join(DELIMITER_END_PARAMETER) + DELIMITER_END_PARAMETER + DELIMITER_END_INSTRUCTION
43
+ end
44
+ end
45
+
46
+ def find(name)
47
+ self.instructions[name] # => {:bytecode => "I", :order => ["variable", "operator", "value"], :parameters => {"variable" => :string_or_integer, "operator" => :string, "value" => :string_or_integer}
48
+ end
49
+
50
+ private
51
+ def string2byte(str)
52
+ value = str.gsub(";", "").gsub("true", "").gsub("false", "")
53
+ if value == "\\x0B"
54
+ "\x0B"
55
+ elsif value.include?("\\x")
56
+ value.sub("\\x", "").to_i(16).chr
57
+ elsif value == "&gt"
58
+ ">"
59
+ elsif value == "&lt"
60
+ "<"
61
+ else
62
+ value
63
+ end
64
+ end
65
+
66
+ # Will return a hash with instruction name and bytecode in array
67
+ # {
68
+ # "if" => {:bytecode => "I", :order => ["variable", "operator", "value"]},
69
+ # "/if" => {:bytecode => "E"}
70
+ # }
71
+ #
72
+ def parse_bytecodes(str)
73
+ last, end_element = nil
74
+ str.split("\n").inject({}) do |hash, str|
75
+ key = str.match(/<xs:element name="(.*)" type="/)
76
+ if key
77
+ last = key[1]
78
+ hash[last] ||= {:order => []}
79
+ end_element = false
80
+ end
81
+
82
+ value = str.match(/<xs:appinfo>(.*)<\/xs:appinfo>/)
83
+ value = value[1] if value
84
+
85
+ if value
86
+ if end_element
87
+ hash["/" + last] = {:order => [], :bytecode => string2byte(value)}
88
+ elsif ! value.include?(";")
89
+ hash[last][:order] = value.split(",")
90
+ else
91
+ end_element = true
92
+ hash[last][:bytecode] = string2byte(value)
93
+ end
94
+ end
95
+ hash
96
+ end
97
+ end
98
+
99
+ # With commands generate by parse_bytecodes:
100
+ # {"if" => ["I"], "/if" => ["E"]}
101
+ # {
102
+ # "if" => {:bytecode => "I", :order => ["variable", "operator", "value"]},
103
+ # "/if" => {:bytecode => "E"}
104
+ # }
105
+ #
106
+ #
107
+ # this method parse parameters and types:
108
+ # {
109
+ # "if" => {
110
+ # :bytecode => "I",
111
+ # :order => ["variable", "operator", "value"],
112
+ # :parameters => {"variable" => :string_or_integer, "operator" => :string, "value" => :string_or_integer}
113
+ # },
114
+ # "/if" => {:bytecode => "E"}
115
+ # }
116
+ def parse_parameters(bytecodes, str)
117
+ commands = bytecodes.dup
118
+ last_byte = nil
119
+ last_param = nil
120
+ str.split("\n").each do |str|
121
+ key = str.match(/<xs:complexType name="(.*)">/)
122
+ last_byte = key[1] if key
123
+
124
+ value = str.match(/<xs:attribute name="(.*)" type/)
125
+ last_param = value[1] if value
126
+
127
+ value = check_type(str)
128
+ if value && last_byte
129
+ commands[last_byte][:parameters] ||= {}
130
+ commands[last_byte][:parameters][last_param] = value
131
+ end
132
+ end
133
+ commands
134
+ end
135
+
136
+ def check_type(str)
137
+ if str.include? "<xs:documentation>"
138
+ if type_string_or_integer?(str)
139
+ :string_or_integer
140
+ elsif type_integer?(str)
141
+ :integer
142
+ else
143
+ :string
144
+ end
145
+ end
146
+ end
147
+
148
+ def type_string_or_integer?(str)
149
+ str.match(/(string ou inteiro|string or integer)/)
150
+ end
151
+
152
+ #def type_string?(str)
153
+ #data = str.match(/(string)/)
154
+ #end
155
+
156
+ def type_integer?(str)
157
+ data = str.match(/(integer|inteiro)/)
158
+ end
159
+ end
160
+ end
161
+
@@ -1,5 +1,5 @@
1
1
 
2
2
  module PosxmlParser
3
- VERSION = "1.3.1"
3
+ VERSION = "2.0.0"
4
4
  end
5
5
 
@@ -14,6 +14,8 @@ Gem::Specification.new do |gem|
14
14
  gem.license = "MIT"
15
15
 
16
16
  gem.files = %w(.gitignore Gemfile Gemfile.lock README.md RELEASE_NOTES.md Rakefile posxml_parser.gemspec lib/posxml_parser/version.rb out/posxml_parser/main.mrb)
17
+ gem.files << "lib/posxml_compiler.rb"
18
+ gem.files << Dir["./lib/posxml_compiler/*.rb"]
17
19
  gem.executables = []
18
20
  gem.test_files = gem.files.grep(%r{^(test)/})
19
21
  gem.require_paths = ["lib"]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: posxml_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - CloudWalk Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-02-15 00:00:00.000000000 Z
11
+ date: 2018-02-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -59,12 +59,28 @@ executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
+ - "./lib/posxml_compiler/en_xsd.rb"
63
+ - "./lib/posxml_compiler/function.rb"
64
+ - "./lib/posxml_compiler/instruction_not_found_error.rb"
65
+ - "./lib/posxml_compiler/jump.rb"
66
+ - "./lib/posxml_compiler/jump_point.rb"
67
+ - "./lib/posxml_compiler/parser.rb"
68
+ - "./lib/posxml_compiler/posxml_compiler_error.rb"
69
+ - "./lib/posxml_compiler/string_size_error.rb"
70
+ - "./lib/posxml_compiler/variable.rb"
71
+ - "./lib/posxml_compiler/variable_limit_error.rb"
72
+ - "./lib/posxml_compiler/variable_name_error.rb"
73
+ - "./lib/posxml_compiler/variable_string_limit_error.rb"
74
+ - "./lib/posxml_compiler/variable_type_error.rb"
75
+ - "./lib/posxml_compiler/variable_undefined_error.rb"
76
+ - "./lib/posxml_compiler/xsd_parser.rb"
62
77
  - ".gitignore"
63
78
  - Gemfile
64
79
  - Gemfile.lock
65
80
  - README.md
66
81
  - RELEASE_NOTES.md
67
82
  - Rakefile
83
+ - lib/posxml_compiler.rb
68
84
  - lib/posxml_parser/version.rb
69
85
  - out/posxml_parser/main.mrb
70
86
  - posxml_parser.gemspec