Class: ExportableGrammar
- Defined in:
- lib/ruby_grammar_builder/grammar.rb
Overview
this has additional behavior to allow for partial grammars to reuse non exported keys
only one may exist per file
Represents a partial Grammar object
Instance Attribute Summary collapse
-
#exports ⇒ Array<Symbol>
Names that will be exported by the grammar partial.
-
#external_repos ⇒ Array<Symbol>
External names the this grammar partial may reference.
-
#parent_grammar ⇒ Grammar
private
Grammars that are a parent to this grammar partial.
Attributes inherited from Grammar
#ending, #name, #repository, #scope_name
Instance Method Summary collapse
-
#[]=(key, value) ⇒ PatternBase, ...
Store a pattern.
-
#export ⇒ self
Modifies the ExportableGrammar to namespace unexported keys.
-
#initialize ⇒ ExportableGrammar
constructor
Initialize a new Exportable Grammar.
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
#initialize ⇒ ExportableGrammar
use Grammar.new_exportable_grammar instead
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
#exports ⇒ Array<Symbol>
Returns 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_repos ⇒ Array<Symbol>
Returns 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_grammar ⇒ Grammar
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
540 541 542 |
# File 'lib/ruby_grammar_builder/grammar.rb', line 540 def parent_grammar @parent_grammar end |
Instance Method Details
#[]=(key, value) ⇒ PatternBase, ...
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
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 |
#export ⇒ self
Modifies the ExportableGrammar to namespace unexported keys
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 = (key.to_s.split(@seed + "_")[1]).to_sym next if @exports.include? 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 |