Sha256: 4bcf7aadbfd43d6bad16e427bfc30e3d2115dd30394d6e3038827e6300c33589

Contents?: true

Size: 1.48 KB

Versions: 23

Compression:

Stored size: 1.48 KB

Contents

# Author::    Eric Crane  (mailto:eric.crane@mac.com)
# Copyright:: Copyright (c) 2019 Eric Crane.  All rights reserved.
#
# The Parser.
# Can parse single line commands or files.
#

module GlooLang
  module Core
    class Parser

      #
      # Set up the parser.
      #
      def initialize( engine )
        @engine = engine
        @engine.log.debug 'parser intialized...'
      end

      #
      # Parse a command from the immediate execution context.
      #
      def parse_immediate( cmd )
        cmd, params = split_params cmd
        params = GlooLang::Core::Tokens.new( params ) if params
        tokens = GlooLang::Core::Tokens.new( cmd )
        dic = GlooLang::Core::Dictionary.instance
        verb = dic.find_verb( tokens.verb )
        return verb.new( @engine, tokens, params ) if verb

        @engine.log.error "Verb '#{tokens.verb}' was not found."
        return nil
      end

      #
      # If additional params were provided, split them out
      # from the token list.
      #
      def split_params( cmd )
        params = nil
        i = cmd.rindex( '(' )
        if i && cmd.strip.end_with?( ')' )
          pstr = cmd[ i + 1..-1 ]
          params = pstr.strip[ 0..-2 ] if pstr
          cmd = cmd[ 0, i - 1 ]
        end
        return cmd, params
      end

      #
      # Parse a command and then run it if it parsed correctly.
      #
      def run( cmd )
        v = parse_immediate( cmd )
        GlooLang::Exec::Runner.go( @engine, v ) if v
      end

    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
gloo-lang-1.4.3 lib/gloo_lang/core/parser.rb
gloo-lang-1.4.2 lib/gloo_lang/core/parser.rb
gloo-lang-1.4.1 lib/gloo_lang/core/parser.rb
gloo-lang-1.4.0 lib/gloo_lang/core/parser.rb
gloo-lang-1.3.2 lib/gloo_lang/core/parser.rb
gloo-lang-1.3.1 lib/gloo_lang/core/parser.rb
gloo-lang-1.3.0 lib/gloo_lang/core/parser.rb
gloo-lang-1.2.8 lib/gloo_lang/core/parser.rb
gloo-lang-1.2.7 lib/gloo_lang/core/parser.rb
gloo-lang-1.2.6 lib/gloo_lang/core/parser.rb
gloo-lang-1.2.5 lib/gloo_lang/core/parser.rb
gloo-lang-1.2.4 lib/gloo_lang/core/parser.rb
gloo-lang-1.2.3 lib/gloo_lang/core/parser.rb
gloo-lang-1.2.2 lib/gloo_lang/core/parser.rb
gloo-lang-1.2.1 lib/gloo_lang/core/parser.rb
gloo-lang-1.2.0 lib/gloo_lang/core/parser.rb
gloo-lang-1.1.0 lib/gloo_lang/core/parser.rb
gloo-lang-1.0.2 lib/gloo_lang/core/parser.rb
gloo-lang-1.0.1 lib/gloo_lang/core/parser.rb
gloo-lang-1.0.0 lib/gloo_lang/core/parser.rb