Permalink
Browse files

lang_python: improved the highlighter, work ok on webpy/ source

  • Loading branch information...
1 parent 7f325aa commit 5bf3511e602fc49e345c74a73478fae03098da85 pad committed Nov 14, 2010
@@ -725,7 +725,7 @@ let info_of_category = function
[`FOREGROUND "MediumPurple1"] ++ info_of_usedef (Def)
| Field (Use2 _) ->
- [`FOREGROUND "MediumPurple3"] ++ info_of_usedef (Use)
+ [`FOREGROUND "MediumPurple2"] ++ info_of_usedef (Use)
| TypeDef usedef -> [`FOREGROUND "YellowGreen"] ++ info_of_usedef usedef
@@ -759,9 +759,9 @@ let info_of_category = function
| Number -> [`FOREGROUND "yellow3";]
- | Boolean -> [`FOREGROUND "cyan";]
+ | Boolean -> [`FOREGROUND "pink3";]
| String -> [`FOREGROUND "green3";]
- | Null -> [`FOREGROUND "cyan";]
+ | Null -> [`FOREGROUND "cyan3";]
@@ -107,6 +107,8 @@ let visit_toplevel
aux_toks xs
(* poor's man identifier tagger *)
+
+ (* defs *)
| T.Tclass ii1::T.TIdent (s, ii2)::xs ->
if not (Hashtbl.mem already_tagged ii2) && lexer_based_tagger
then tag ii2 (Class (Def2 fake_no_def2));
@@ -118,6 +120,55 @@ let visit_toplevel
then tag ii2 (Function (Def2 fake_no_def2));
aux_toks xs
+
+ (* uses *)
+
+ | T.TIdent (s, ii1)::T.TDot ii2::T.TIdent (s3, ii3)::T.TOParen(ii4)::xs ->
+ if not (Hashtbl.mem already_tagged ii3) && lexer_based_tagger
+ then begin
+ tag ii3 (Method (Use2 fake_no_use2));
+ if not (Hashtbl.mem already_tagged ii1)
+ then tag ii1 (Local Use);
+ end;
+ aux_toks xs
+
+ | T.TIdent (s, ii1)::T.TOParen(ii2)::xs ->
+ if not (Hashtbl.mem already_tagged ii1) && lexer_based_tagger
+ then tag ii1 (Function (Use2 fake_no_use2));
+ aux_toks xs
+
+ | T.TIdent (s, ii1)::T.TDot ii2::T.TIdent (s3, ii3)::xs ->
+ (match xs with
+ | (T.TDot _)::_ ->
+
+ if not (Hashtbl.mem already_tagged ii3) && lexer_based_tagger
+ then tag ii3 (Field (Use2 fake_no_use2));
+
+ if not (Hashtbl.mem already_tagged ii1)
+ then tag ii1 (Local Use);
+
+ aux_toks (T.TIdent (s3, ii3)::xs)
+
+ | _ ->
+ if not (Hashtbl.mem already_tagged ii3) && lexer_based_tagger
+ then begin
+ tag ii3 (Field (Use2 fake_no_use2));
+ (* TODO *)
+ if not (Hashtbl.mem already_tagged ii1)
+ then tag ii1 (Local Use);
+ end;
+ aux_toks xs
+ )
+
+ | T.TIdent (s, ii1)::xs ->
+ if s =~ "[a-z]" then begin
+ if not (Hashtbl.mem already_tagged ii1) && lexer_based_tagger
+ then tag ii1 (Local (Use));
+ end;
+ aux_toks xs
+
+
+
| x::xs ->
aux_toks xs
in
@@ -164,6 +215,11 @@ let visit_toplevel
| T.TComplex (_, ii) ->
tag ii Number
+
+ | T.TLongString (s,ii) ->
+ (* most of the time they are used as documentation strings *)
+ tag ii Comment
+
(* keywords *)
| T.Tdef ii | T.Tlambda ii ->
tag ii Keyword
@@ -196,10 +252,12 @@ let visit_toplevel
| T.Twith ii
| T.Tdel ii
| T.Tglobal ii
+ -> tag ii Keyword
+
| T.Tnot ii
| T.Tand ii
| T.Tor ii
- -> tag ii Keyword
+ -> tag ii BuiltinBoolean
(* symbols *)
@@ -255,8 +313,12 @@ let visit_toplevel
-> tag ii Punctuation
- | T.TIdent (s, ii)
- -> ()
+ | T.TIdent (s, ii) ->
+ match s with
+ | "None" -> tag ii Null
+ | "True" | "False" -> tag ii Boolean
+ | "self" -> tag ii KeywordObject
+ | _ -> ()
);
@@ -217,12 +217,12 @@ rule token = parse
| ['u''U']? "'''" {
let info = tokinfo lexbuf in
let s = string_triple_quote lexbuf in
- TString (s, info +> Parse_info.tok_add_s (s ^ "'''"))
+ TLongString (s, info +> Parse_info.tok_add_s (s ^ "'''"))
}
| ['u''U']? '"' '"' '"' {
let info = tokinfo lexbuf in
let s = string_triple_double_quote lexbuf in
- TString (s, info +> Parse_info.tok_add_s (s ^ "\"\"\""))
+ TLongString (s, info +> Parse_info.tok_add_s (s ^ "\"\"\""))
}
(* TODO: the rules for the raw string are not exactly the same;
@@ -242,12 +242,12 @@ rule token = parse
| ("r" | "ur" | "R" | "UR" | "Ur" | "uR") "'''" {
let info = tokinfo lexbuf in
let s = string_triple_quote lexbuf in
- TString (s, info +> Parse_info.tok_add_s (s ^ "'"))
+ TLongString (s, info +> Parse_info.tok_add_s (s ^ "'"))
}
| ("r" | "ur" | "R" | "UR" | "Ur" | "uR") '"' '"' '"' {
let info = tokinfo lexbuf in
let s = string_triple_double_quote lexbuf in
- TString (s, info +> Parse_info.tok_add_s (s ^ "'"))
+ TLongString (s, info +> Parse_info.tok_add_s (s ^ "'"))
}
@@ -273,7 +273,7 @@ and string_quote = parse
| "'" { "" }
| [^ '\'' '\n']* { let s = tok lexbuf in s ^ string_quote lexbuf }
- | escapeseq { let s = tok lexbuf in string_quote lexbuf }
+ | escapeseq { let s = tok lexbuf in s ^ string_quote lexbuf }
| eof { pr2 "LEXER: end of file in string_quote"; "'"}
| _ { let s = tok lexbuf in
@@ -285,7 +285,7 @@ and string_double_quote = parse
| '"' { "" }
| [^ '\"' '\n']* { let s = tok lexbuf in s ^ string_double_quote lexbuf }
- | escapeseq { let s = tok lexbuf in string_double_quote lexbuf }
+ | escapeseq { let s = tok lexbuf in s ^ string_double_quote lexbuf }
| eof { pr2 "LEXER: end of file in string_double_quote"; "'"}
@@ -298,8 +298,8 @@ and string_triple_quote = parse
| "'''" { "" }
| [^ '\\' '\'' ]* { let s = tok lexbuf in s ^ string_triple_quote lexbuf }
- | escapeseq { let s = tok lexbuf in string_triple_quote lexbuf }
- | "'" { let s = tok lexbuf in string_triple_quote lexbuf }
+ | escapeseq { let s = tok lexbuf in s ^ string_triple_quote lexbuf }
+ | "'" { let s = tok lexbuf in s ^ string_triple_quote lexbuf }
| eof { pr2 "LEXER: end of file in string_triple_quote"; "'"}
| _ { let s = tok lexbuf in
@@ -311,8 +311,8 @@ and string_triple_double_quote = parse
| '"' '"' '"' { "" }
| [^ '\\' '"' ]* { let s = tok lexbuf in s ^ string_triple_double_quote lexbuf }
- | escapeseq { let s = tok lexbuf in string_triple_double_quote lexbuf }
- | '"' { let s = tok lexbuf in string_triple_double_quote lexbuf }
+ | escapeseq { let s = tok lexbuf in s ^ string_triple_double_quote lexbuf }
+ | '"' { let s = tok lexbuf in s ^ string_triple_double_quote lexbuf }
| eof { pr2 "LEXER: end of file in string_triple_double_quote"; "'"}
| _ { let s = tok lexbuf in
@@ -47,6 +47,7 @@ open Ast_python
%token <string * Ast_python.info> TComplex
%token <string * Ast_python.info> TChar
%token <string * Ast_python.info> TString
+%token <string * Ast_python.info> TLongString
%token <string * Ast_python.info> TIdent
@@ -87,6 +87,7 @@ let visitor_info_of_tok f = function
| TComplex (s, ii) -> TComplex (s, f ii)
| TChar (s, ii) -> TChar (s, f ii)
| TString (s, ii) -> TString (s, f ii)
+ | TLongString (s, ii) -> TLongString (s, f ii)
| TIdent (s, ii) -> TIdent (s, f ii)

0 comments on commit 5bf3511

Please sign in to comment.