Class: ExportableGrammar

Inherits:
Grammar show all
Defined in:
lib/ruby_grammar_builder/grammar.rb

Overview

Note:

this has additional behavior to allow for partial grammars to reuse non exported keys

Note:

only one may exist per file

Represents a partial Grammar object

Instance Attribute Summary collapse

Attributes inherited from Grammar

#ending, #name, #repository, #scope_name

Instance Method Summary collapse

Methods inherited from Grammar

#[], #auto_version, fromTmLanguage, #generate, import, #import, new_exportable_grammar, #parseTokenSyntax, plugins, register_linter, register_transform, remove_plugin, #run_post_transform_stage, #run_pre_transform_stage, #save_to, #tokenMatching

Constructor Details

#initializeExportableGrammar

Note:

Initialize a new Exportable Grammar



546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
# File 'lib/ruby_grammar_builder/grammar.rb', line 546

def initialize
    # skip: initialize, new, and new_exportable_grammar
    location = caller_locations(3, 1).first
    # and the first 5 bytes of the hash to get the seed
    # will not be unique if multiple exportable grammars are created in the same file
    # Don't do that
    @seed = Digest::MD5.hexdigest(File.basename(location.path))[0..10]
    super(
        name: "export",
        scope_name: "export"
    )

    if @@export_grammars[location.path].is_a? Hash
        return if @@export_grammars[location.path][:line] == location.lineno

        raise "Only one export grammar is allowed per file"
    end
    @@export_grammars[location.path] = {
        line: location.lineno,
        grammar: self,
    }

    @parent_grammar = []
end

Instance Attribute Details

#exportsArray<Symbol>

Returns names that will be exported by the grammar partial.

Returns:

  • (Array<Symbol>)

    names that will be exported by the grammar partial



531
532
533
# File 'lib/ruby_grammar_builder/grammar.rb', line 531

def exports
  @exports
end

#external_reposArray<Symbol>

Returns external names the this grammar partial may reference.

Returns:

  • (Array<Symbol>)

    external names the this grammar partial may reference



533
534
535
# File 'lib/ruby_grammar_builder/grammar.rb', line 533

def external_repos
  @external_repos
end

#parent_grammarGrammar

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Grammars that are a parent to this grammar partial

Returns:



540
541
542
# File 'lib/ruby_grammar_builder/grammar.rb', line 540

def parent_grammar
  @parent_grammar
end

Instance Method Details

#[]=(key, value) ⇒ PatternBase, ...

Note:

grammar partials cannot constribute to $initial_context

Store a pattern

A pattern must be stored in the grammar for it to appear in the final grammar

The special key :$initial_context is the pattern that will be matched at the beginning of the document or whenever the root of the grammar is to be matched

Parameters:

  • key (Symbol)

    The key to store the pattern in

  • value (PatternBase, Symbol, Array<PatternBase, Symbol>)

    the pattern to store

Returns:



576
577
578
579
580
581
582
583
584
# File 'lib/ruby_grammar_builder/grammar.rb', line 576

def []=(key, value)
    if key.to_s == "$initial_context"
        puts "ExportGrammar cannot store to $initial_context"
        raise "See error above"
    end
    super(key, value)

    parent_grammar.each { |g| g.import self }
end

#exportself

Modifies the ExportableGrammar to namespace unexported keys

Returns:

  • (self)


591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
# File 'lib/ruby_grammar_builder/grammar.rb', line 591

def export
    @repository.transform_keys! do |key|
        next if [:$initial_context, :$base, :$self].include? key

        # convert all repository keys to a prefixed version unless in exports
        if key.to_s.start_with? @seed
            # if exports has changed remove the seed
            bare_key = (key.to_s.split(@seed + "_")[1]).to_sym
            next bare_key if @exports.include? bare_key

            next key
        end

        next key if @exports.include? key

        (@seed + "_" + key.to_s).to_sym
    end
    # prefix all include symbols unless in external_repos or exports
    @repository.transform_values! { |v| fixupValue(v) }
    # ensure the grammar does not refer to a symbol not in repository or external_repos
    # ensure the grammar has all keys named in exports
    exports.each do |key|
        unless @repository.has_key? key
            raise "#{key} is exported but is missing in the repository"
        end
    end
    self
end