[tyndur-devel] [PATCH] kedit: Syntax-Hochlichtung für Ruby hinzugefügt
Alexander Kluth ·
Syntax-Hochlichtung für Ruby, Dürfte den Patrick Pokatilo freuen. Jetzt fehlt nur noch Ruby selbst ;-) Signed-off-by: Alexander Kluth <hartmut at tyndur.org> --- src/modules/pas/kedit/kedit_main.pas | 2 + src/modules/pas/kedit/kedit_tui.pas | 4 +- src/modules/pas/kedit/syntax_ruby.pas | 234 +++++++++++++++++++++++++++++++++ 3 files changed, 239 insertions(+), 1 deletions(-) create mode 100644 src/modules/pas/kedit/syntax_ruby.pas diff --git a/src/modules/pas/kedit/kedit_main.pas b/src/modules/pas/kedit/kedit_main.pas index 69732ad..a284101 100644 --- a/src/modules/pas/kedit/kedit_main.pas +++ b/src/modules/pas/kedit/kedit_main.pas @@ -116,6 +116,8 @@ begin state^.extension := 'pm'; end else if RightStr(filename, 3) = '.py' then begin state^.extension := 'py'; + end else if RightStr(filename, 3) = '.rb' then begin + state^.extension := 'rb'; end; SetSyntaxHighlighting(state); diff --git a/src/modules/pas/kedit/kedit_tui.pas b/src/modules/pas/kedit/kedit_tui.pas index 7e6cd40..66719ab 100644 --- a/src/modules/pas/kedit/kedit_tui.pas +++ b/src/modules/pas/kedit/kedit_tui.pas @@ -61,7 +61,7 @@ implementation uses crt, strutils, sysutils, - syntax, syntax_c, syntax_pas, syntax_intel, syntax_atandt, syntax_perl, syntax_python; + syntax, syntax_c, syntax_pas, syntax_intel, syntax_atandt, syntax_perl, syntax_python, syntax_ruby; resourcestring rsMenuBar = 'F2: Speichern, F3: Laden, F8: Syntax, F10: Beenden, EINFG: Einfügen/Ersetzen'; @@ -326,6 +326,8 @@ begin highlighter := TSyntax_Perl.create; end else if state^.extension = 'py' then begin highlighter := TSyntax_Python.create; + end else if state^.extension = 'rb' then begin + highlighter := TSyntax_Ruby.create; end; end; diff --git a/src/modules/pas/kedit/syntax_ruby.pas b/src/modules/pas/kedit/syntax_ruby.pas new file mode 100644 index 0000000..d23ed84 --- /dev/null +++ b/src/modules/pas/kedit/syntax_ruby.pas @@ -0,0 +1,234 @@ +(* + * Copyright (c) 2011 The tyndur Project. All rights reserved. + * + * This code is derived from software contributed to the tyndur Project + * by Alexander Kluth. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + *) + +unit syntax_ruby; +{$mode ObjFPC} + +interface + +uses syntax; + +type + TSyntax_Ruby = class(TSyntax) + public + procedure StartLine(s: String; state: integer); override; + function Next: TSyntaxChange; override; + end; + + +implementation +const + Keywords_Ruby: Array [1..38] of String = ( + 'alias', 'and', 'BEGIN', 'begin', 'break', + 'case', 'class', 'def', 'defined?', 'do', + 'else', 'elsif', 'END', 'end', 'ensure', + 'false', 'for', 'if', 'in', 'module', + 'next', 'nil', 'not', 'or', 'redo', + 'rescue', 'retry', 'return', 'self', 'super', + 'then', 'true', 'undef', 'unless', 'until', + 'when', 'while', 'yield' + ); + + Vars_Ruby: Array [1..40] of String = ( + '$!', '$@', '$&', '$`', '$''', + '$+', '$1', '$~', '$=', '$/', + '$\', '$,', '$;', '$.', '$<', + '$>', '$_', '$0', '$*', '$$', + '$?', '$:', '$"', '$DEBUG', '$FILENAME', + '$LOAD_PATH', '$stderr', '$stdin', '$stdout', '$VERBOSE', + '$-0', '$-a', '$-d', '$-F', '$-i', + '$-I', '$-l', '$-p', '$-v', '$-w' + ); + +procedure TSyntax_Ruby.StartLine(s: String; state: integer); +begin + line := s; + f_state := state; + pos := 1; + + case f_state of + 1: color := syn_comment; + 2: color := syn_comment; + 3: color := syn_number; + 4: color := syn_string; + 5: color := syn_string_special; + 6: color := syn_keyword; + 7: color := syn_type; + 8: color := syn_label; + else color := syn_other; + end; +end; + + +function TSyntax_Ruby.Next: TSyntaxChange; +var + c: char; + tmp: integer; +begin + Next.posY := 0; + Next.color := color; + + while pos <= length(line) do begin + c := line[pos]; + + case f_state of + + 0: { Normaler Text } + case c of + '/': + begin + f_state := 2; + exit(Highlight(syn_string_special)); + end; + + '0' .. '9', '-', '+': + begin + tmp := MatchesNumber(line, pos); + if tmp <> 0 then begin + Next := Highlight(syn_number); + Inc(pos, tmp); + f_state := 3; + exit; + end; + end; + + '#': + begin + if Copy(line, 1, pos - 1) = Space(pos - 1) then begin + f_state := 1; + exit(Highlight(syn_comment)); + end; + end; + + '"': + begin + Next := Highlight(syn_string); + Inc(pos); + f_state := 4; + exit; + end; + ' ': + begin + if MatchesTrailingSpace(line, pos) then begin + Next := Highlight(syn_trailing_space); + pos := length(line) + 1; + exit; + end; + end; + + else + begin + tmp := MatchesKeyword(line, pos, Keywords_Ruby); + if tmp <> 0 then begin + Next := Highlight(syn_keyword); + Inc(pos, tmp); + f_state := 6; + exit; + end; + + tmp := MatchesKeyword(line, pos, Vars_Ruby); + if tmp <> 0 then begin + Next := Highlight(syn_type); + Inc(pos, tmp); + f_state := 7; + exit; + end; + + tmp := MatchesType(line, pos); + if tmp <> 0 then begin + Next := Highlight(syn_type); + Inc(pos, tmp); + f_state := 7; + exit; + end; + + tmp := MatchesLabel(line, pos); + if tmp <> 0 then begin + Next := Highlight(syn_label); + Inc(pos, tmp); + f_state := 8; + exit; + end; + end; + end; + + 1: { Kommentar bis Zeilenende } + begin + pos := length(line); + break; + end; + + 2: { Regulärer Ausdruck } + if Matches(line, pos, '/') then begin + f_state := 0; + Inc(pos, 2); + exit(Highlight(syn_other)); + end; + + 3, 6, 7, 8: { Ende eines gefaerbten Worts } + begin + f_state := 0; + exit(Highlight(syn_other)); + end; + + 4: { String } + case c of + '\': + begin + f_state := 5; + Next := Highlight(syn_string_special); + Inc(pos); + exit; + end; + + '"': + begin + f_state := 0; + Inc(pos); + exit(Highlight(syn_other)); + end; + end; + + 5: { Escaptes Zeichen in einem String } + begin + f_state := 4; + Inc(pos); + exit(Highlight(syn_string)); + end; + + end; + + Inc(pos); + end; + + if f_state in [1, 3, 4, 5] then begin + f_state := 0; + end; +end; + +end. -- 1.7.3.4