summaryrefslogtreecommitdiff
path: root/lib/coderay/scanners/php.rb
diff options
context:
space:
mode:
authormurphy <murphy@rubychan.de>2009-10-20 11:12:26 +0000
committermurphy <murphy@rubychan.de>2009-10-20 11:12:26 +0000
commit9f6e54e92820754d4f05e7d6ef556c11c5351411 (patch)
tree3a612819bb121418f05fae076e7640228c36824f /lib/coderay/scanners/php.rb
parenta8163c39206fd11ef30ef8efb2c73a6f6a97e424 (diff)
downloadcoderay-9f6e54e92820754d4f05e7d6ef556c11c5351411.tar.gz
Updated PHP scanner (#36)
* highlighting of class and function definitions * improved HTML/PHP detection * heredocs (simple) * a new test for classes
Diffstat (limited to 'lib/coderay/scanners/php.rb')
-rw-r--r--lib/coderay/scanners/php.rb50
1 files changed, 42 insertions, 8 deletions
diff --git a/lib/coderay/scanners/php.rb b/lib/coderay/scanners/php.rb
index 700c7cb..bfbc642 100644
--- a/lib/coderay/scanners/php.rb
+++ b/lib/coderay/scanners/php.rb
@@ -184,7 +184,7 @@ module Scanners
HTML_INDICATOR = /<!DOCTYPE html|<(?:html|body|div|p)[> ]/i
- IDENTIFIER = /[a-z_\x80-\xFF][a-z0-9_\x80-\xFF]*/i
+ IDENTIFIER = /[a-z_\x7f-\xFF][a-z0-9_\x7f-\xFF]*/i
VARIABLE = /\$#{IDENTIFIER}/
OPERATOR = /
@@ -205,7 +205,7 @@ module Scanners
states = [:initial]
if match?(RE::PHP_START) || # starts with <?
- (match?(/\s*<(?i:\w|\?xml)/) && exist?(RE::PHP_START)) || # starts with HTML tag and contains <?
+ (match?(/\s*<\S/) && exist?(RE::PHP_START)) || # starts with tag and contains <?
exist?(RE::HTML_INDICATOR)
# is PHP inside HTML, so start with HTML
else
@@ -253,7 +253,10 @@ module Scanners
kind = :label
elsif kind == :ident && match =~ /^[A-Z]/
kind = :constant
- # TODO: function and class definitions
+ elsif kind == :reserved && match == 'class'
+ states << :class_expected
+ elsif kind == :reserved && match == 'function'
+ states << :function_expected
end
elsif scan(/(?:\d+\.\d*|\d*\.\d+)(?:e[-+]?\d+)?|\d+e[-+]?\d+/i)
@@ -277,11 +280,17 @@ module Scanners
states.push :dqstring
# TODO: Heredocs
- # elsif match = scan(/<<</ + IDENTIFIER)
- # tokens << [:open, :string]
- # heredocdelim = match[RE::IDENTIFIER]
- # kind = :delimiter
- # states.push :heredocstring
+ # See http://de2.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
+ elsif match = scan(/<<<(#{RE::IDENTIFIER})/o)
+ tokens << [:open, :string]
+ heredocdelim = Regexp.escape self[1]
+ tokens << [match, :delimiter]
+ next if eos?
+ tokens << [scan_until(/\n(?=#{heredocdelim};?$)|\z/), :content]
+ next if eos?
+ tokens << [scan(/#{heredocdelim}/), :delimiter]
+ tokens << [:close, :string]
+ next
elsif scan RE::VARIABLE
kind = :local_variable
@@ -379,6 +388,31 @@ module Scanners
elsif scan(/\$/)
kind = :content
end
+
+ when :class_expected
+ if scan(/\s+/)
+ kind = :space
+ elsif match = scan(/#{RE::IDENTIFIER}/o)
+ kind = :class
+ states.pop
+ else
+ states.pop
+ next
+ end
+
+ when :function_expected
+ if scan(/\s+/)
+ kind = :space
+ elsif scan(/&/)
+ kind = :operator
+ elsif match = scan(/#{RE::IDENTIFIER}/o)
+ kind = :function
+ states.pop
+ else
+ states.pop
+ next
+ end
+
else
raise_inspect 'Unknown state!', tokens, states
end