[tyndur-devel] [PATCH] kedit: Hochlichtung für Haskell hinzugefügt
Alexander Kluth ·
Hochlichtung für Haskell. Dürfte Patrick Pokatilo umsomehr freuen. Ob ich Langeweile habe? Ein wenig. Aber so ein paar Lockerungsübungen abseits von kirc sei einem doch gegönnt ;-) 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_haskell.pas | 230 ++++++++++++++++++++++++++++++ 3 files changed, 235 insertions(+), 1 deletions(-) create mode 100644 src/modules/pas/kedit/syntax_haskell.pas diff --git a/src/modules/pas/kedit/kedit_main.pas b/src/modules/pas/kedit/kedit_main.pas index a284101..14af27a 100644 --- a/src/modules/pas/kedit/kedit_main.pas +++ b/src/modules/pas/kedit/kedit_main.pas @@ -118,6 +118,8 @@ begin state^.extension := 'py'; end else if RightStr(filename, 3) = '.rb' then begin state^.extension := 'rb'; + end else if RightStr(filename, 3) = '.hs' then begin + state^.extension := 'hs'; end; SetSyntaxHighlighting(state); diff --git a/src/modules/pas/kedit/kedit_tui.pas b/src/modules/pas/kedit/kedit_tui.pas index 66719ab..350485c 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_ruby; + syntax, syntax_c, syntax_pas, syntax_intel, syntax_atandt, syntax_perl, syntax_python, syntax_ruby, syntax_haskell; resourcestring rsMenuBar = 'F2: Speichern, F3: Laden, F8: Syntax, F10: Beenden, EINFG: Einfügen/Ersetzen'; @@ -328,6 +328,8 @@ begin highlighter := TSyntax_Python.create; end else if state^.extension = 'rb' then begin highlighter := TSyntax_Ruby.create; + end else if state^.extension = 'hs' then begin + highlighter := TSyntax_Haskell.create; end; end; diff --git a/src/modules/pas/kedit/syntax_haskell.pas b/src/modules/pas/kedit/syntax_haskell.pas new file mode 100644 index 0000000..ba0bc7b --- /dev/null +++ b/src/modules/pas/kedit/syntax_haskell.pas @@ -0,0 +1,230 @@ +(* + * 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_haskell; +{$mode ObjFPC} + +interface + +uses syntax; + +type + TSyntax_Haskell = class(TSyntax) + public + procedure StartLine(s: String; state: integer); override; + function Next: TSyntaxChange; override; + end; + + +implementation +const + Keywords_Haskell: Array [1..29] of String = ( + 'as', 'case', 'class', 'data', 'instance', + 'default', 'deriving', 'do', 'forall', 'foreign', + 'hiding', 'if', 'then', 'else', 'of', + 'import', 'infix', 'infixl', 'infixr', 'let', + 'in', 'mdo', 'module', 'newtype', 'proc', + 'qualified', 'rec', 'type', 'where' + ); + + Vars_Haskell: Array [1..27] of String = ( + '!', '''', '"', '*', '?', + '??', '#', '-', '-<', + '-<<', '->', '::', ';', '<-', + '=', '=>', '>', '@', '[', + ']', '\', '_', '`', '{', + '}', '|', '~' + ); + +procedure TSyntax_Haskell.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_Haskell.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 + '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 Matches(line, pos, '--') then begin + f_state := 1; + exit(Highlight(syn_comment)); + end; + end; + '{': + begin + if Matches(line, pos, '{-') then begin + f_state := 2; + 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_Haskell); + if tmp <> 0 then begin + Next := Highlight(syn_keyword); + Inc(pos, tmp); + f_state := 6; + exit; + end; + + tmp := MatchesKeyword(line, pos, Vars_Haskell); + 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: { Mehrzeilige Kommentar } + 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