Module: Ionize::Php

Defined in:
lib/ionize.rb,
lib/ionize/tokenizer.rb,
lib/ionize/translate.rb,
lib/ionize/environment.rb,
lib/ionize/translate/debug.rb,
lib/ionize/translate/rewrites.rb,
lib/ionize/translate/rewritable.rb,
lib/ionize/translate/statements.rb,
lib/ionize/translate/translator.rb,
lib/ionize/environment/php_array.rb,
lib/ionize/translate/php_to_ruby.rb,
lib/ionize/environment/application.rb,
lib/ionize/translate/function_args.rb,
lib/ionize/translate/if_statements.rb,
lib/ionize/translate/rails_for_php.rb,
lib/ionize/translate/term_statements.rb,
lib/ionize/translate/multiple_statements.rb,
lib/ionize/translate/switch_case_statements.rb,
lib/ionize/translate/composite_string_statements.rb,
lib/ionize/parser.rb

Defined Under Namespace

Modules: Environment, Translate Classes: Grammar, Tokenizer

Constant Summary collapse

CharacterLimit =
50000
Config =
{
  "magic_quotes_gpc"     => true,
  "magic_quotes_runtime" => true
}

Class Method Summary collapse

Class Method Details

.to_ast(string) ⇒ Object

Returns a Dhaka AST of a string



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ionize.rb', line 20

def self.to_ast(string)
  # TODO: Error handling could be better..
  # Maybe another time

  if string.length > CharacterLimit
    raise "Your input is #{string.length}. Ionize::Php::Parser currently cannot handle inputs larger than #{CharacterLimit}, due to Dhaka's memory usage"
  end
  
  tokens = Php.to_tokens(string)
  result = Php::CompiledParser.parse(tokens)
  
  if result.is_a? Dhaka::ParseErrorResult
    error = %Q{ 
Could not parse token.
Token => #{result.unexpected_token.inspect}

}
    error << string.slice(result.unexpected_token.input_position, 40) rescue nil
    raise error
  elsif result.is_a? Dhaka::TokenizerErrorResult
    raise result
  end

  result
end

.to_raw_sexp(string) ⇒ Object

A stripped down version of what Dhaka hands us



66
67
68
# File 'lib/ionize.rb', line 66

def self.to_raw_sexp(string)
  Php::Translate.flatten(Php.to_ast(string))
end

.to_ruby(string) ⇒ Object

Abracadbra!



71
72
73
# File 'lib/ionize.rb', line 71

def self.to_ruby(string)
  Php::Translate::Php2Ruby.new.process(Php.to_sexp(string))
end

.to_sexp(string) ⇒ Object

An S-expression of the string



61
62
63
# File 'lib/ionize.rb', line 61

def self.to_sexp(string)
  Php::Translate.translate(Php.to_ast(string))
end

.to_tokens(string) ⇒ Object

Tokens from Dhaka’s tokenizer



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ionize.rb', line 47

def self.to_tokens(string)
  require 'ionize/tokenizer'
  tokenizer = Php::Tokenizer.new(string)
  tokenizer.switch_to :php unless string =~ /<\?php/i
  tokens = tokenizer.run

  if tokens.is_a? Dhaka::TokenizerErrorResult
    raise "Could not tokenize at index #{tokens.unexpected_char_index}\n" + string.slice(tokens.unexpected_char_index, 40).inspect
  end

  tokens
end