iparser 1.1.5 → 1.1.6

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 (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +27 -22
  3. data/lib/iparser.rb +67 -32
  4. data/lib/iparser/version.rb +1 -1
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 051ef5d38afe13cb8520bd3d614c7ed901162b36
4
- data.tar.gz: 525de30addbade95e26236e7501a71065881b9ca
3
+ metadata.gz: 97d7e3125f043d5c5717ceaf8419cf62acce7ae1
4
+ data.tar.gz: e52c91bbf66725980e8be57de10f1b72b6dc9407
5
5
  SHA512:
6
- metadata.gz: d5060ec8998ed3ba971cb8f3471fba3a6a836d2fe14118323917371267090f6ead2363d9e357faee8d35266a94d0f397d255d5883d2d4969cfcd0a626fc1f794
7
- data.tar.gz: 84b75688e07b04918b1d39e5f940e69e2b8b98fd8cc3183eb69f541f708d21d1a0c79ddc28904c0851b836ce254b2895e6fbf8682e4055f3d41a586db0dcd146
6
+ metadata.gz: 2004e1f5c41dc9e7a9c70aab0ebc7946581d7acbde7ac8852943674c8fdd26a8cc7bc855ea0a053bbf28bfdd2b1b727ad91630ec0ef697b96f020aedf90bc19b
7
+ data.tar.gz: 1610d3c67e56eb93107791a0d5b853297835a93faf0784a27b12e35fb26dc91d707ddfd745d9a536da8cdb354baf8146a4b331d57a809e1a76a2d634095f51d6
data/README.md CHANGED
@@ -42,6 +42,11 @@ parser.addstate ps_idle
42
42
  parser.addstate ps_cline
43
43
  parser.addstate ps_cblock
44
44
 
45
+ # Add branch indexes to 'comment-line' and 'comment-block' state.
46
+ # From 'idle' we can branch to 'comment-line' or 'comment-block' states.
47
+ ps_idle.branches << parser.state_index( ps_cline )
48
+ ps_idle.branches << parser.state_index( ps_cblock )
49
+
45
50
  # Describe 'comment-line' state.
46
51
  # Set template for entry this state (String or Regexp).
47
52
  ps_cline.entry << '/'
@@ -49,14 +54,6 @@ ps_cline.entry << '/'
49
54
  ps_cline.entry << '/'
50
55
  # Set template for leave this state (String or Regexp).
51
56
  ps_cline.leave << /[\n\r]/
52
- # Add handler to 'commaent-line' state.
53
- ps_cline.init( method(:doc_init) )
54
- ps_cline.handler( method(:doc_handler) )
55
- ps_cline.fini( method(:doc_fini) )
56
- # Add branch indexes to 'comment-line' and 'comment-block' state.
57
- # From 'idle' we can branch to 'comment-line' or 'comment-block' states.
58
- ps_idle.branches << parser.state_index( ps_cline )
59
- ps_idle.branches << parser.state_index( ps_cblock )
60
57
 
61
58
  # Describe 'comment-block' state.
62
59
  # Set template for entry this state (String or Regexp).
@@ -83,15 +80,15 @@ Check each state.
83
80
  **NOTE**: Type `'\\'` for input `'\'`.
84
81
 
85
82
  Each state has the following templates:
86
- 1. __entry__ - used for set condition (template) to entry state.
87
- 2. __leave__ - used for set condition (template) to leave state (return to previous state).
88
- 3. __ignore__ - used for set symbols to ignoring.
83
+ * `entry` - used for set condition (template) to entry state.
84
+ * `leave` - used for set condition (template) to leave state (return to previous state).
85
+ * `ignore` - used for set symbols to ignoring.
89
86
 
90
87
  After successfully check, you can add handler to each state.
91
88
  Each state has the following handlers:
92
- 1. __init__ - is state contructor (called when entry to state).
93
- 2. __fini__ - is state destructor (called when leave state).
94
- 3. __handler__ - is state handler (called every time, is still in state).
89
+ * `init` - is state contructor (called when entry to state).
90
+ * `fini` - is state destructor (called when leave state).
91
+ * `handler` - is state handler (called every time, is still in state).
95
92
 
96
93
  Each handler can return the following values: __nil__ - nothing is done (method `.parse` return `true`)
97
94
  and __any__ values for break parsing (method `.parse` return `false`). For break parsing process you
@@ -114,6 +111,9 @@ We create the following handlers:
114
111
  For `comment-block` state set ignore char - `*`, and handler don't called to this chars.
115
112
  The result is a file with the following content of 'parser_example.rb':
116
113
 
114
+ Constructors and destructors handlers will be getting array chars with own templates.
115
+ For example `doc_init` getting follow array chars: `['/', '/', '/']`
116
+
117
117
  *source № 2*:
118
118
  ```ruby
119
119
  require 'iparser'
@@ -131,15 +131,15 @@ $fout = File.new( 'index.html', 'w' )
131
131
 
132
132
  # Create initializer method for parser-states.
133
133
  def doc_init ( str )
134
- $fout.print "<p>"
134
+ $fout.print "<p>"; return nil
135
135
  end
136
136
  # Create handler method for parser-states.
137
137
  def doc_handler ( c )
138
- $fout.print c
138
+ $fout.print c; return nil
139
139
  end
140
140
  # Create finalizer method for parser-states.
141
141
  def doc_fini ( str )
142
- $fout.puts "</p>"
142
+ $fout.puts "</p>"; return nil
143
143
  end
144
144
 
145
145
  # Create parser-machine object.
@@ -156,6 +156,11 @@ parser.addstate ps_idle
156
156
  parser.addstate ps_cline
157
157
  parser.addstate ps_cblock
158
158
 
159
+ # Add branch indexes to 'comment-line' and 'comment-block' state.
160
+ # From 'idle' we can branch to 'comment-line' or 'comment-block' states.
161
+ ps_idle.branches << parser.state_index( ps_cline )
162
+ ps_idle.branches << parser.state_index( ps_cblock )
163
+
159
164
  # Describe 'comment-line' state.
160
165
  # Set template for entry this state (String or Regexp).
161
166
  ps_cline.entry << '/'
@@ -167,10 +172,6 @@ ps_cline.leave << /[\n\r]/
167
172
  ps_cline.init( method(:doc_init) )
168
173
  ps_cline.handler( method(:doc_handler) )
169
174
  ps_cline.fini( method(:doc_fini) )
170
- # Add branch indexes to 'comment-line' and 'comment-block' state.
171
- # From 'idle' we can branch to 'comment-line' or 'comment-block' states.
172
- ps_idle.branches << parser.state_index( ps_cline )
173
- ps_idle.branches << parser.state_index( ps_cblock )
174
175
 
175
176
  # Describe 'comment-block' state.
176
177
  # Set template for entry this state (String or Regexp).
@@ -283,12 +284,16 @@ end
283
284
 
284
285
  Details information for each patch.
285
286
 
287
+ ##### 1.1.6
288
+ * Fixed bug in analyse `leave` template (incorrectly handling, if `entry` and `leave` is begin with identical symbol).
289
+ * Now `ignore` template enabled for all handlers (init, fini, handler).
290
+
286
291
  ##### 1.1.5
287
292
  * Add method `.current_state` for return name (String) of current state in parser-machine.
288
293
 
289
294
  ##### 1.1.4
290
295
  * Add method `.state_index` for return index of parser-state.
291
- * Add analyes result of any handler (init, fini, handler).
296
+ * Add analyse result of any handler (init, fini, handler).
292
297
  * Now `entry` and `leave` templates you can set a string or regular expression.
293
298
 
294
299
  ## Development
data/lib/iparser.rb CHANGED
@@ -331,6 +331,7 @@ module Iparser
331
331
  return -1
332
332
  end
333
333
  backtag = 1
334
+ mindex = -1
334
335
  else
335
336
  #
336
337
  # Нет совпадения, но если уже часть сравнений
@@ -380,7 +381,7 @@ module Iparser
380
381
  end
381
382
  #
382
383
  # Анализ количества совпадений.
383
- case mcount
384
+ case (mcount + backtag)
384
385
  #
385
386
  # нет совпадений.
386
387
  when 0
@@ -389,8 +390,12 @@ module Iparser
389
390
  # однозначное совпадение, но весь массив шаблонов
390
391
  # еще не пройден.
391
392
  when 1
392
- @matchstate[:state] = 1
393
- @matchstate[:index] = mindex
393
+ if mindex == -1 then
394
+ @matchstate[:state] = 2
395
+ else
396
+ @matchstate[:state] = 1
397
+ @matchstate[:index] = mindex
398
+ end
394
399
  return -2
395
400
  #
396
401
  # нет однозначного соответствия.
@@ -398,7 +403,7 @@ module Iparser
398
403
  return -2
399
404
  end
400
405
 
401
- # Состояние точно определено.
406
+ # Состояние точно определено (переход в перед).
402
407
  # :state = 1
403
408
  when 1
404
409
  i = @matchstate[:index]
@@ -429,6 +434,36 @@ module Iparser
429
434
  @states[i].ientry = 0
430
435
  @matchstate[:state] = 0
431
436
  return -3
437
+
438
+ # Состояние точно определено (возврат назад).
439
+ # :state = 2
440
+ when 2
441
+ if cmp( state.leave, state.ileave ) then
442
+ state.ileave = state.ileave.next
443
+ #
444
+ # Возврат в предыдущее состояние.
445
+ if state.ileave >= state.leave.size then
446
+ @matchstate[:state] = 0
447
+ return -1
448
+ end
449
+ return -2
450
+ end
451
+ #
452
+ # Нет совпадения, но если уже часть сравнений
453
+ # успешна, то возможно входной символ совпадает
454
+ # с предыдущими, уже совпавшими символами,
455
+ # т.е. как откат в режиме <wait>.
456
+ #
457
+ i = checkback( state.leave, state.ileave )
458
+
459
+ if i != -1 then
460
+ state.ileave = i.next
461
+ return -2
462
+ end
463
+ state.ileave = 0
464
+ @matchstate[:state] = 0
465
+ return -3
466
+
432
467
  end # case @matchstate
433
468
  end
434
469
 
@@ -446,6 +481,11 @@ module Iparser
446
481
  #
447
482
  @buffer << c
448
483
 
484
+ # Задан шаблон для игнорирования символов.
485
+ if @chain.last.ignore.size > 0 then
486
+ return retval if @chain.last.ignore.include?(c)
487
+ end
488
+
449
489
  # Проверка переходов в другие состояния.
450
490
  r = classify( @chain.last )
451
491
 
@@ -487,36 +527,31 @@ module Iparser
487
527
  # буфер надо обработать.
488
528
  @buffer.each do |ch|
489
529
  @parserstate = 'miss'
490
- tag = true
491
- if @chain.last.ignore.size > 0 then
492
- tag = false if @chain.last.ignore.include?(ch)
493
- end
494
- if tag == true then
495
- r = @chain.last.run_handler( ch )
496
- #
497
- # Анализ результата обработки состояния.
498
- case r.class.to_s
499
- #
500
- # Fixnum - переход на любое состояние (индекс).
501
- when 'Fixnum'
502
- if( (r >= 0) && (r < @states.size) ) then
503
- @chain << @states[r]
504
- reset( )
505
- @parserstate = 'hardset'
506
- else
507
- raise TypeError, "Method <#{@chain.last.statename}> return incorrectly index."
508
- end
509
- #
510
- # nil - ничего не возвращает.
511
- when 'NilClass'
512
- #
513
- # else - расценивается как ошибка обработки.
514
- # обработка ложится на плечи разработчика.
530
+
531
+ r = @chain.last.run_handler( ch )
532
+ #
533
+ # Анализ результата обработки состояния.
534
+ case r.class.to_s
535
+ #
536
+ # Fixnum - переход на любое состояние (индекс).
537
+ when 'Fixnum'
538
+ if( (r >= 0) && (r < @states.size) ) then
539
+ @chain << @states[r]
540
+ reset( )
541
+ @parserstate = 'hardset'
515
542
  else
516
- @parserstate = 'error'
517
- retval = false
518
- break
543
+ raise TypeError, "Method <#{@chain.last.statename}> return incorrectly index."
519
544
  end
545
+ #
546
+ # nil - ничего не возвращает.
547
+ when 'NilClass'
548
+ #
549
+ # else - расценивается как ошибка обработки.
550
+ # обработка ложится на плечи разработчика.
551
+ else
552
+ @parserstate = 'error'
553
+ retval = false
554
+ break
520
555
  end
521
556
  end
522
557
  @buffer = []
@@ -1,3 +1,3 @@
1
1
  module Iparser
2
- VERSION = "1.1.5"
2
+ VERSION = "1.1.6"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iparser
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.5
4
+ version: 1.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - gera-gas
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-12-07 00:00:00.000000000 Z
11
+ date: 2015-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler