weskit 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -111,7 +111,7 @@ To get Weskit::WML::Elements container with references to all b elements you can
111
111
  ```
112
112
 
113
113
  This code takes advantage of method_missing as well and if you can't use
114
- it that (because of method name conflicts) its equivalent can be used:
114
+ it (because of method name conflicts) its equivalent can be used:
115
115
 
116
116
  ```ruby
117
117
  root.find(:a).find(:b)
@@ -126,7 +126,7 @@ Weskit::WML objects dosn't have any representation code coupled. Special class o
126
126
  used to handle their display. Most commonly you will use plain text representation (default formatter)
127
127
  or terminal friendly colorful version of it (color formatter). Most of WML objects provide formatter
128
128
  seter to replace formatter associated with them but you can as well change default formatter globally
129
- by calling Formatter.default= method.
129
+ by calling Formatter.default= setter method.
130
130
 
131
131
  Whatever representation you need (JSON, XML, YAML) creating new formaters shouldn't
132
132
  be hard. Take look at Weskit::WML::Formatters module for further reference on that matter.
data/Rakefile CHANGED
@@ -4,7 +4,7 @@ require 'rspec/core/rake_task'
4
4
  RSpec::Core::RakeTask.new 'spec'
5
5
  task :default => :spec
6
6
 
7
- namespace :kpeg do
7
+ namespace :parser do
8
8
  desc 'Re-build KPEGParser'
9
9
  task :rebuild do
10
10
  puts %x(kpeg -fsvo lib/weskit/wml/kpeg_parser.rb lib/weskit/wml/grammars/wml.kpeg)
@@ -1,3 +1,3 @@
1
1
  module Weskit
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
@@ -44,7 +44,9 @@ module Weskit::WML::Formatters
44
44
  def text_value attribute
45
45
  string = escape text attribute
46
46
  string = "#@quote#{string}#@quote"
47
- string = "#@underscore#{string}" if attribute.translatable?
47
+
48
+ return "#@underscore#{string}" if attribute.translatable?
49
+ string
48
50
  end
49
51
 
50
52
  def value attribute
@@ -22,7 +22,7 @@
22
22
 
23
23
  # Rules ========================================================================
24
24
 
25
- id = < /[a-z][a-z_]*/i > { text }
25
+ id = < /[a-z][0-9a-z_]*/i > { text }
26
26
  ids = ids:i1 - ',' - ids:i2 { i1 + i2 }
27
27
  | id:i { [i] }
28
28
 
@@ -31,7 +31,7 @@ items = items:i1 items:i2 { i1 + i2 }
31
31
  | item:i { (i.is_a? Array) ? i : [i] }
32
32
 
33
33
  contents = items?:i { i.to_a }
34
-
34
+
35
35
  # Attributes -------------------------------------------------------------------
36
36
 
37
37
  single_attr = - id:n - '=' - val:v - eol
@@ -88,4 +88,4 @@ blk_lines = (- eol)*
88
88
 
89
89
  # Root -------------------------------------------------------------------------
90
90
 
91
- root = contents:c { Root.new.push *c }
91
+ root = contents:c { c.empty? ? nil : Root.new.push(*c) }
@@ -366,13 +366,13 @@ class KPEGParser
366
366
  # :stopdoc:
367
367
  def setup_foreign_grammar; end
368
368
 
369
- # id = < /[a-z][a-z_]*/i > { text }
369
+ # id = < /[a-z][0-9a-z_]*/i > { text }
370
370
  def _id
371
371
 
372
372
  _save = self.pos
373
373
  while true # sequence
374
374
  _text_start = self.pos
375
- _tmp = scan(/\A(?i-mx:[a-z][a-z_]*)/)
375
+ _tmp = scan(/\A(?i-mx:[a-z][0-9a-z_]*)/)
376
376
  if _tmp
377
377
  text = get_text(_text_start)
378
378
  end
@@ -569,8 +569,8 @@ class KPEGParser
569
569
  return _tmp
570
570
  end
571
571
 
572
- # attr = - id:n - "=" - val:v - eol { Attribute.new n, *v }
573
- def _attr
572
+ # single_attr = - id:n - "=" - val:v - eol { Attribute.new n, *v }
573
+ def _single_attr
574
574
 
575
575
  _save = self.pos
576
576
  while true # sequence
@@ -624,11 +624,11 @@ class KPEGParser
624
624
  break
625
625
  end # end sequence
626
626
 
627
- set_failed_rule :_attr unless _tmp
627
+ set_failed_rule :_single_attr unless _tmp
628
628
  return _tmp
629
629
  end
630
630
 
631
- # multiple_attrs = - ids:n - "=" - vals:v - eol { n.reduce(Array.new) do |attrs, name| value = v.shift or [nil] attrs << Attribute.new(name, *value) end }
631
+ # multiple_attrs = - ids:n - "=" - vals:v - eol { n.reduce(Array.new) do |attrs, name| value =v.shift or [nil] attrs << Attribute.new(name, *value) end }
632
632
  def _multiple_attrs
633
633
 
634
634
  _save = self.pos
@@ -676,7 +676,7 @@ class KPEGParser
676
676
  break
677
677
  end
678
678
  @result = begin; n.reduce(Array.new) do |attrs, name|
679
- value = v.shift or [nil]
679
+ value =v.shift or [nil]
680
680
  attrs << Attribute.new(name, *value)
681
681
  end ; end
682
682
  _tmp = true
@@ -690,7 +690,7 @@ class KPEGParser
690
690
  return _tmp
691
691
  end
692
692
 
693
- # attribute = blk_lines attr:a blk_lines { a }
693
+ # attribute = blk_lines single_attr:a blk_lines { a }
694
694
  def _attribute
695
695
 
696
696
  _save = self.pos
@@ -700,7 +700,7 @@ class KPEGParser
700
700
  self.pos = _save
701
701
  break
702
702
  end
703
- _tmp = apply(:_attr)
703
+ _tmp = apply(:_single_attr)
704
704
  a = @result
705
705
  unless _tmp
706
706
  self.pos = _save
@@ -1461,7 +1461,7 @@ class KPEGParser
1461
1461
  return _tmp
1462
1462
  end
1463
1463
 
1464
- # root = contents:c { Root.new.push *c }
1464
+ # root = contents:c { c.empty? ? nil : Root.new.push(*c) }
1465
1465
  def _root
1466
1466
 
1467
1467
  _save = self.pos
@@ -1472,7 +1472,7 @@ class KPEGParser
1472
1472
  self.pos = _save
1473
1473
  break
1474
1474
  end
1475
- @result = begin; Root.new.push *c ; end
1475
+ @result = begin; c.empty? ? nil : Root.new.push(*c) ; end
1476
1476
  _tmp = true
1477
1477
  unless _tmp
1478
1478
  self.pos = _save
@@ -1485,14 +1485,14 @@ class KPEGParser
1485
1485
  end
1486
1486
 
1487
1487
  Rules = {}
1488
- Rules[:_id] = rule_info("id", "< /[a-z][a-z_]*/i > { text }")
1488
+ Rules[:_id] = rule_info("id", "< /[a-z][0-9a-z_]*/i > { text }")
1489
1489
  Rules[:_ids] = rule_info("ids", "(ids:i1 - \",\" - ids:i2 { i1 + i2 } | id:i { [i] })")
1490
1490
  Rules[:_item] = rule_info("item", "(attribute | attributes | element)")
1491
1491
  Rules[:_items] = rule_info("items", "(items:i1 items:i2 { i1 + i2 } | item:i { (i.is_a? Array) ? i : [i] })")
1492
1492
  Rules[:_contents] = rule_info("contents", "items?:i { i.to_a }")
1493
- Rules[:_attr] = rule_info("attr", "- id:n - \"=\" - val:v - eol { Attribute.new n, *v }")
1494
- Rules[:_multiple_attrs] = rule_info("multiple_attrs", "- ids:n - \"=\" - vals:v - eol { n.reduce(Array.new) do |attrs, name| value = v.shift or [nil] attrs << Attribute.new(name, *value) end }")
1495
- Rules[:_attribute] = rule_info("attribute", "blk_lines attr:a blk_lines { a }")
1493
+ Rules[:_single_attr] = rule_info("single_attr", "- id:n - \"=\" - val:v - eol { Attribute.new n, *v }")
1494
+ Rules[:_multiple_attrs] = rule_info("multiple_attrs", "- ids:n - \"=\" - vals:v - eol { n.reduce(Array.new) do |attrs, name| value =v.shift or [nil] attrs << Attribute.new(name, *value) end }")
1495
+ Rules[:_attribute] = rule_info("attribute", "blk_lines single_attr:a blk_lines { a }")
1496
1496
  Rules[:_attributes] = rule_info("attributes", "blk_lines multiple_attrs:a blk_lines { a }")
1497
1497
  Rules[:_code] = rule_info("code", "< /<<.*?>>/m > { [strip_chars(text, 2), {:code => true}] }")
1498
1498
  Rules[:_in_brackets] = rule_info("in_brackets", "< /\\(.*?\\)/m > { [strip_chars(text, 1)] }")
@@ -1514,7 +1514,7 @@ class KPEGParser
1514
1514
  Rules[:__hyphen_] = rule_info("-", "sp*")
1515
1515
  Rules[:_sp_lf] = rule_info("sp_lf", "(sp | eol)*")
1516
1516
  Rules[:_blk_lines] = rule_info("blk_lines", "(- eol)*")
1517
- Rules[:_root] = rule_info("root", "contents:c { Root.new.push *c }")
1517
+ Rules[:_root] = rule_info("root", "contents:c { c.empty? ? nil : Root.new.push(*c) }")
1518
1518
  # :startdoc:
1519
1519
  end
1520
1520
 
@@ -15,7 +15,7 @@ module Weskit::WML
15
15
  begin
16
16
  str = open(uri).read
17
17
  rescue
18
- raise Errrors::ReadError, "Couldn't open URI"
18
+ raise Errors::ReadError, "Couldn't open URI"
19
19
  ensure
20
20
  str = Zlib::GzipReader.new(StringIO.new str).read rescue str
21
21
  end
@@ -26,6 +26,7 @@ module Weskit::WML
26
26
  private
27
27
 
28
28
  def parse data
29
+ data = remove_directives data
29
30
  parser = KPEGParser.new "#{data}\n"
30
31
 
31
32
  unless parser.parse
@@ -35,6 +36,17 @@ module Weskit::WML
35
36
 
36
37
  parser.result
37
38
  end
39
+
40
+ # It removes directives because:
41
+ # - prepreocessor isn't supported anyway
42
+ # - adding rules for directives hinders performance
43
+ # - it will remove lines nested inside multiline strings
44
+ # if they match preprocessor directive pattern
45
+ def remove_directives data
46
+ ary = data.split "\n"
47
+ ary.reject! {|i| i.match /\A\s*(\#|\{)/}
48
+ ary.join "\n"
49
+ end
38
50
  end
39
51
  end
40
52
  end
@@ -4,8 +4,19 @@ describe Weskit::WML::Parser do
4
4
  include WMLHelpers
5
5
 
6
6
  describe 'parse' do
7
+ specify 'ignore preprocessor' do
8
+ data = <<-DOC
9
+ {~/path/to.cfg}
10
+ #undef SOME_STUFF
11
+ DOC
12
+
13
+ parsed = Weskit::WML::Parser.string data
14
+ parsed.should be_nil
15
+ end
16
+
7
17
  specify 'empty lines' do
8
- Weskit::WML::Parser.string document_with_empty_lines
18
+ parsed = Weskit::WML::Parser.string document_with_empty_lines
19
+ parsed.should_not be_nil
9
20
  end
10
21
 
11
22
  context 'attribute' do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: weskit
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 0
10
- version: 0.2.0
9
+ - 1
10
+ version: 0.2.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - f6p
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-10-05 00:00:00 Z
18
+ date: 2012-10-10 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: kpeg