parslet-ext 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/parslet_ext.rb +50 -0
  3. metadata +60 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 73bb714b74fb9c9739928d624e0fd1f7b89c133a
4
+ data.tar.gz: 6f21911c0d3209621efb20e68f18ef6c2b0da9a4
5
+ SHA512:
6
+ metadata.gz: cd34e9316d0834a45003f8b73c5fb6dcdd6825549574f46c8970bad3140e9939f5985ecf6c2f81e7fedb5ed9c3dea70e4e0ef597b86dbbcdae294ee0419487d7
7
+ data.tar.gz: 2dd5de7d2a7e1f6aee310725207b0b9a1a75bdec13b67124e8825b5349cb42e91900a291a6eadac1000bba29a6d4bc82c4badd9dea7bbc48c0094bebcf6b87dd
@@ -0,0 +1,50 @@
1
+ require 'parslet'
2
+
3
+ # Parslet DSL extension for capturing the input source
4
+ class CaptureSource < Parslet::Atoms::Capture
5
+ # Ugly hack to capture the source string that was parsed
6
+ def apply(source, context, consume_all)
7
+ before = source.instance_variable_get(:@str).rest
8
+ success, value = result = super(source, context, consume_all)
9
+ if success
10
+ # Save the portion of the source string
11
+ after = source.instance_variable_get(:@str).rest
12
+ source_str = before[0..(before.length - after.length - 1)]
13
+ value[(name.to_s + '_source').to_sym] = source_str
14
+ end
15
+
16
+ result
17
+ end
18
+ end
19
+
20
+ # Modify named captures to allow arrays
21
+ class Parslet::Atoms::Named < Parslet::Atoms::Base
22
+ def initialize(parslet, name, array = false)
23
+ super()
24
+ @parslet = parslet
25
+ @name = name
26
+ @array = array
27
+ end
28
+
29
+ private
30
+
31
+ # Optionally wrap the produced single value in an array
32
+ def produce_return_value(val)
33
+ flatval = flatten(val, true)
34
+ flatval = [flatval] if @array && val.last == [:repetition]
35
+ { name => flatval }
36
+ end
37
+ end
38
+
39
+ # Extend the DSL to with some additional ways to capture the output
40
+ module Parslet::Atoms::DSL
41
+ # Like #as, but ensures that the result is always an array
42
+ def as_array(name)
43
+ Parslet::Atoms::Named.new(self, name, true)
44
+ end
45
+
46
+ # Capture some output along with the source string
47
+ def capture_source(name)
48
+ CaptureSource.new(self, name)
49
+ end
50
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: parslet-ext
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Michael Mior
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: parslet
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ description: Adds some useful functions to Parlset which the author has decided not
28
+ include in the core
29
+ email: michael.mior@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - lib/parslet_ext.rb
35
+ homepage: https://github.com/michaelmior/parslet-ext
36
+ licenses:
37
+ - GPLv3
38
+ metadata: {}
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 2.4.6
56
+ signing_key:
57
+ specification_version: 4
58
+ summary: Extensions to the Parslet parsing library
59
+ test_files: []
60
+ has_rdoc: