Module: Flexr

Defined in:
lib/flexr.rb,
lib/flexr/ir.rb,
lib/flexr/cli.rb,
lib/flexr/dsl.rb,
lib/flexr/lexer.rb,
lib/flexr/errors.rb,
lib/flexr/options.rb,
lib/flexr/runtime.rb,
lib/flexr/version.rb,
lib/flexr/importer.rb,
lib/flexr/generated.rb,
lib/flexr/generator.rb,
lib/flexr/rake_task.rb,
lib/flexr/regexp/ast.rb,
lib/flexr/diagnostics.rb,
lib/flexr/codegen/base.rb,
lib/flexr/runtime/core.rb,
lib/flexr/unicode/data.rb,
lib/flexr/automaton/dfa.rb,
lib/flexr/automaton/nfa.rb,
lib/flexr/codegen/table.rb,
lib/flexr/configuration.rb,
lib/flexr/regexp/parser.rb,
lib/flexr/runtime/token.rb,
lib/flexr/codegen/direct.rb,
lib/flexr/runtime/buffer.rb,
lib/flexr/runtime/errors.rb,
lib/flexr/action_resolver.rb,
lib/flexr/artifact_writer.rb,
lib/flexr/automaton/accel.rb,
lib/flexr/automaton/types.rb,
lib/flexr/unicode/version.rb,
lib/flexr/regexp/tokenizer.rb,
lib/flexr/runtime/location.rb,
lib/flexr/unicode/property.rb,
lib/flexr/regexp/char_class.rb,
lib/flexr/regexp/normalizer.rb,
lib/flexr/unicode/case_fold.rb,
lib/flexr/automaton/analysis.rb,
lib/flexr/automaton/compiler.rb,
lib/flexr/codegen/firstmatch.rb,
lib/flexr/regexp/unsupported.rb,
lib/flexr/source/passthrough.rb,
lib/flexr/source/static_eval.rb,
lib/flexr/automaton/minimizer.rb,
lib/flexr/runtime/interpreter.rb,
lib/flexr/source/prism_reader.rb,
lib/flexr/codegen/table_packer.rb,
lib/flexr/unicode/utf8_splitter.rb,
lib/flexr/regexp/tokenizer.flexr.rb,
lib/flexr/unicode/data/properties.rb,
lib/flexr/automaton/byte_class_set.rb,
lib/flexr/unicode/reference_regexp.rb,
lib/flexr/unicode/data/case_folding.rb,
lib/flexr/automaton/backend_cost_model.rb

Defined Under Namespace

Modules: ActionResolver, ArtifactWriter, Automaton, CLI, Codegen, Configuration, DSL, Diagnostics, Generated, IR, Regexp, Runtime, Source, Unicode Classes: CompileError, Diagnostic, DiagnosticSet, Error, FrozenSpecificationError, Generator, Importer, LexError, Lexer, Options, RakeTask, StaticResolutionError, UnsupportedRegexpError

Constant Summary collapse

VERSION =
"1.1.0"

Class Method Summary collapse

Class Method Details

.compile_pattern(pattern, options: {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/flexr/runtime.rb', line 43

def compile_pattern(pattern, options: {})
  regexp = pattern.is_a?(::Regexp) ? pattern : ::Regexp.new(pattern.to_s)
  encoding = regexp.encoding == Encoding::BINARY ? Encoding::BINARY : Encoding::UTF_8
  rule = IR::Rule.new(index: 0, patterns: [regexp], action: :skip, states: [:initial])
  state = IR::State.new(name: :initial, inclusive: true, id: 0)
  spec = IR::Spec.new(
    class_name: "Pattern", backend: :table, token_kind: :array,
    encoding: encoding, options: options, declared_tokens: [],
    states: { initial: state }, rules: [rule], eof_rules: {}, verbatim: nil
  )
  Automaton::Compiler.new(spec).compile.machines.fetch(:initial).dfa
end

.parse_pattern(pattern, options: {}) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/flexr/runtime.rb', line 62

def parse_pattern(pattern, options: {})
  regexp = pattern.is_a?(::Regexp) ? pattern : ::Regexp.new(pattern.to_s)
  encoding = regexp.encoding == Encoding::BINARY ? Encoding::BINARY : Encoding::UTF_8
  ast = Regexp::Parser.new(regexp.source, options: regexp.options, encoding: encoding,
                           unicode: options[:unicode] == true).parse
  Regexp::Normalizer.new(ast, encoding: encoding, options: regexp.options).normalize
end

.reference_pattern?(regexp, unicode: false) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
60
# File 'lib/flexr/runtime.rb', line 56

def reference_pattern?(regexp, unicode: false)
  return true if regexp.source.match?(/\\[pP]\{/) || regexp.source.match?(/\[:(?:\^)?[a-z]+:\]/)

  unicode && regexp.encoding != Encoding::BINARY && regexp.source.match?(/\\[dDwWsS]/)
end