diff options
| author | mokha <mokha@cisco.com> | 2019-05-14 17:58:45 -0600 |
|---|---|---|
| committer | mokha <mokha@cisco.com> | 2019-05-14 17:58:45 -0600 |
| commit | f5c56951e3efc1044ebcfed38b0f0fe63d4bd01b (patch) | |
| tree | a860b32f6b94431f71d38b51ceab1ca5b293e04f | |
| parent | b2c6315f54b54dd28bfc32d5b5b3b60020a6d4bb (diff) | |
split out parsers into separate files
| -rw-r--r-- | lib/mpeg.rb | 13 | ||||
| -rw-r--r-- | lib/mpeg/alternative.rb | 12 | ||||
| -rw-r--r-- | lib/mpeg/base.rb | 23 | ||||
| -rw-r--r-- | lib/mpeg/input.rb | 18 | ||||
| -rw-r--r-- | lib/mpeg/lookahead.rb | 8 | ||||
| -rw-r--r-- | lib/mpeg/parser.rb | 120 | ||||
| -rw-r--r-- | lib/mpeg/re.rb | 15 | ||||
| -rw-r--r-- | lib/mpeg/repitition.rb | 19 | ||||
| -rw-r--r-- | lib/mpeg/sequence.rb | 12 | ||||
| -rw-r--r-- | lib/mpeg/slice.rb | 6 | ||||
| -rw-r--r-- | lib/mpeg/str.rb | 16 |
11 files changed, 141 insertions, 121 deletions
diff --git a/lib/mpeg.rb b/lib/mpeg.rb index d4ae8f9..c53cf89 100644 --- a/lib/mpeg.rb +++ b/lib/mpeg.rb @@ -1,5 +1,16 @@ -require "mpeg/parser" require "mpeg/version" +require "mpeg/base" + +require "mpeg/alternative" +require "mpeg/input" +require "mpeg/lookahead" +require "mpeg/parser" +require "mpeg/parser" +require "mpeg/re" +require "mpeg/repitition" +require "mpeg/sequence" +require "mpeg/slice" +require "mpeg/str" module Mpeg class Error < StandardError; end diff --git a/lib/mpeg/alternative.rb b/lib/mpeg/alternative.rb new file mode 100644 index 0000000..148b265 --- /dev/null +++ b/lib/mpeg/alternative.rb @@ -0,0 +1,12 @@ +module Mpeg + class Alternative < Base + def initialize(left, right) + @left = left + @right = right + end + + def call(input) + @left.call(input) || @right.call(input) + end + end +end diff --git a/lib/mpeg/base.rb b/lib/mpeg/base.rb new file mode 100644 index 0000000..33c7e6a --- /dev/null +++ b/lib/mpeg/base.rb @@ -0,0 +1,23 @@ +module Mpeg + class Base + def parse(string) + call(Input.new(string)) + end + + def repeat(min = 0, max = nil) + Repitition.new(self, min, max) + end + + def |(parser) + Alternative.new(self, parser) + end + + def >>(parser) + Sequence.new(self, parser) + end + + def absent? + Lookahead.new(self, false) + end + end +end diff --git a/lib/mpeg/input.rb b/lib/mpeg/input.rb new file mode 100644 index 0000000..03ef6d7 --- /dev/null +++ b/lib/mpeg/input.rb @@ -0,0 +1,18 @@ +module Mpeg + class Input + def initialize(string) + @scanner = StringScanner.new(string) + end + + def matches?(pattern) + @scanner.match?(pattern) + end + + def consume(characters) + position = @scanner.pos + slice = @scanner.scan(/(.|$){#{characters}}/m) + + Slice.new(position, slice) + end + end +end diff --git a/lib/mpeg/lookahead.rb b/lib/mpeg/lookahead.rb new file mode 100644 index 0000000..7fcbf3d --- /dev/null +++ b/lib/mpeg/lookahead.rb @@ -0,0 +1,8 @@ +module Mpeg + class Lookahead < Base + def initialize(parser, positive) + @parser = parser + @positive = positive + end + end +end diff --git a/lib/mpeg/parser.rb b/lib/mpeg/parser.rb index 1a313d4..9295fcf 100644 --- a/lib/mpeg/parser.rb +++ b/lib/mpeg/parser.rb @@ -1,124 +1,4 @@ module Mpeg - class Base - def parse(string) - call(Input.new(string)) - end - - def repeat(min = 0, max = nil) - Repitition.new(self, min, max) - end - - def |(parser) - Alternative.new(self, parser) - end - - def >>(parser) - Sequence.new(self, parser) - end - - def absent? - Lookahead.new(self, false) - end - end - - class Slice - def initialize(position, string) - end - end - - class Input - def initialize(string) - @scanner = StringScanner.new(string) - end - - def matches?(pattern) - @scanner.match?(pattern) - end - - def consume(characters) - position = @scanner.pos - slice = @scanner.scan(/(.|$){#{characters}}/m) - - Slice.new(position, slice) - end - end - - class Lookahead < Base - def initialize(parser, positive) - @parser = parser - @positive = positive - end - end - - class Sequence < Base - def initialize(left, right) - @left = left - @right = right - end - - def call(input) - @left.call(input) && @right.call(input) - end - end - - class Alternative < Base - def initialize(left, right) - @left = left - @right = right - end - - def call(input) - @left.call(input) || @right.call(input) - end - end - - class Repitition < Base - def initialize(parser, min, max) - @parser = parser - @min = min - @max = max - end - - def call(input) - occurrences = 0 - loop do - break unless @parser.call(input) - occurrences += 1 - return false if @max && occurrences > @max - end - occurrences >= @min - end - end - - class Re < Base - def initialize(regex) - @regex = Regexp.new("[#{regex}]", Regexp::MULTILINE) - end - - def call(input) - if input.matches?(@regex) - input.consume(1) - return true - end - false - end - end - - class Str < Base - def initialize(string) - @length = string.size - @regex = Regexp.new(Regexp.escape(string)) - end - - def call(input) - if input.matches?(@regex) - input.consume(1) - return true - end - false - end - end - class Parser def parse(string) root.parse(string) diff --git a/lib/mpeg/re.rb b/lib/mpeg/re.rb new file mode 100644 index 0000000..61df57c --- /dev/null +++ b/lib/mpeg/re.rb @@ -0,0 +1,15 @@ +module Mpeg + class Re < Base + def initialize(regex) + @regex = Regexp.new("[#{regex}]", Regexp::MULTILINE) + end + + def call(input) + if input.matches?(@regex) + input.consume(1) + return true + end + false + end + end +end diff --git a/lib/mpeg/repitition.rb b/lib/mpeg/repitition.rb new file mode 100644 index 0000000..9903b60 --- /dev/null +++ b/lib/mpeg/repitition.rb @@ -0,0 +1,19 @@ +module Mpeg + class Repitition < Base + def initialize(parser, min, max) + @parser = parser + @min = min + @max = max + end + + def call(input) + occurrences = 0 + loop do + break unless @parser.call(input) + occurrences += 1 + return false if @max && occurrences > @max + end + occurrences >= @min + end + end +end diff --git a/lib/mpeg/sequence.rb b/lib/mpeg/sequence.rb new file mode 100644 index 0000000..e3823de --- /dev/null +++ b/lib/mpeg/sequence.rb @@ -0,0 +1,12 @@ +module Mpeg + class Sequence < Base + def initialize(left, right) + @left = left + @right = right + end + + def call(input) + @left.call(input) && @right.call(input) + end + end +end diff --git a/lib/mpeg/slice.rb b/lib/mpeg/slice.rb new file mode 100644 index 0000000..cb6c7aa --- /dev/null +++ b/lib/mpeg/slice.rb @@ -0,0 +1,6 @@ +module Mpeg + class Slice + def initialize(position, string) + end + end +end diff --git a/lib/mpeg/str.rb b/lib/mpeg/str.rb new file mode 100644 index 0000000..f5266e9 --- /dev/null +++ b/lib/mpeg/str.rb @@ -0,0 +1,16 @@ +module Mpeg + class Str < Base + def initialize(string) + @length = string.size + @regex = Regexp.new(Regexp.escape(string)) + end + + def call(input) + if input.matches?(@regex) + input.consume(1) + return true + end + false + end + end +end |
