| 1 | ############################################################################### |
|---|
| 2 | # Name: cobra.py # |
|---|
| 3 | # Purpose: Define Cobra syntax for highlighting and other features # |
|---|
| 4 | # Author: Eric Sellon <eric.sellon@gmail.com> # |
|---|
| 5 | # Copyright: (c) 2009 Eric Sellon <eric.sellon@gmail.com> # |
|---|
| 6 | # License: wxWindows License # |
|---|
| 7 | ############################################################################### |
|---|
| 8 | |
|---|
| 9 | """ |
|---|
| 10 | FILE: cobra.py |
|---|
| 11 | AUTHOR: Eric Sellon |
|---|
| 12 | @summary: Lexer configuration module for Cobra. |
|---|
| 13 | |
|---|
| 14 | """ |
|---|
| 15 | |
|---|
| 16 | __author__ = "Eric Sellon <eric.sellon@gmail.com>" |
|---|
| 17 | __svnid__ = "$Id: python.py 59312 2009-05-25 14:35:51Z CJP $" |
|---|
| 18 | __revision__ = "$Revision: 59312 $" |
|---|
| 19 | |
|---|
| 20 | #-----------------------------------------------------------------------------# |
|---|
| 21 | # Dependancies |
|---|
| 22 | import keyword |
|---|
| 23 | |
|---|
| 24 | #-----------------------------------------------------------------------------# |
|---|
| 25 | |
|---|
| 26 | #---- Keyword Specifications ----# |
|---|
| 27 | |
|---|
| 28 | # Indenter keywords |
|---|
| 29 | INDENT_KW = (u"def", u"if", u"success", u"else", u"for", u"while", u"expect", |
|---|
| 30 | u"class", u"invariant", u"cue", u"try", u"except", u"finally", |
|---|
| 31 | u"body", u"shared", u"test", u"namespace", u"branch", u"post", u"on") |
|---|
| 32 | UNINDENT_KW = (u"return", u"raise", u"break", u"continue", |
|---|
| 33 | u"pass") |
|---|
| 34 | |
|---|
| 35 | # Cobra Keywords |
|---|
| 36 | KEYWORDS = ('use', 'import', 'namespace', 'enum', 'class', 'inherits', 'implements', 'interface', 'mixin', 'struct', |
|---|
| 37 | 'extend', 'where', 'must', 'be', 'callable', 'adds', 'cue', 'invariant', 'def', 'sig', 'as', 'get', 'set', |
|---|
| 38 | 'pro', 'var', 'const', 'from', 'has', 'body', 'test', 'shared', 'is', 'event', 'of', 'inlined', 'abstract', |
|---|
| 39 | 'extern', 'fake', 'internal', 'new', 'nonvirtual', 'override', 'partial', 'public', 'private', 'protected', |
|---|
| 40 | 'virtual', 'bool', 'char', 'int', 'uint', 'decimal', 'float', 'dynamic', 'same', 'number', 'require', |
|---|
| 41 | 'ensure', 'old', 'implies', 'assert', 'branch', 'on', 'off', 'expect', 'if', 'else', 'using', 'while', |
|---|
| 42 | 'post', 'for', 'break', 'continue', 'try', 'catch', 'success', 'finally', 'throw', 'pass', 'print', 'stop', |
|---|
| 43 | 'trace', 'return', 'yield', 'listen', 'ignore', 'raise', 'end', 'do', 'ref', 'this', 'base', 'to', 'to\\?', |
|---|
| 44 | 'and', 'or', 'not', 'any', 'every', 'all', 'each', 'in', 'passthrough', 'true', 'false', 'nil', 'vari', 'out', |
|---|
| 45 | 'inout', 'objc', 'except') |
|---|
| 46 | #KEYWORDS.extend(['True', 'False', 'None', 'self']) |
|---|
| 47 | PY_KW = (0, u" ".join(KEYWORDS)) |
|---|
| 48 | |
|---|
| 49 | # Highlighted builtins |
|---|
| 50 | try: |
|---|
| 51 | import __builtin__ |
|---|
| 52 | BUILTINS = dir(__builtin__) |
|---|
| 53 | except: |
|---|
| 54 | BUILTINS = list() |
|---|
| 55 | #BUILTINS.append('self') |
|---|
| 56 | BUILTINS = list(set(BUILTINS)) |
|---|
| 57 | |
|---|
| 58 | PY_BIN = (1, u" ".join(sorted(BUILTINS))) |
|---|
| 59 | |
|---|
| 60 | #---- Syntax Style Specs ----# |
|---|
| 61 | SYNTAX_ITEMS = [ ('STC_P_DEFAULT', 'default_style'), |
|---|
| 62 | ('STC_P_CHARACTER', 'char_style'), |
|---|
| 63 | ('STC_P_CLASSNAME', 'class_style'), |
|---|
| 64 | ('STC_P_COMMENTBLOCK', 'comment_style'), |
|---|
| 65 | ('STC_P_COMMENTLINE', 'comment_style'), |
|---|
| 66 | ('STC_P_DECORATOR', 'decor_style'), |
|---|
| 67 | ('STC_P_DEFNAME', 'keyword3_style'), |
|---|
| 68 | ('STC_P_IDENTIFIER', 'default_style'), |
|---|
| 69 | ('STC_P_NUMBER', 'number_style'), |
|---|
| 70 | ('STC_P_OPERATOR', 'operator_style'), |
|---|
| 71 | ('STC_P_STRING', 'string_style'), |
|---|
| 72 | ('STC_P_STRINGEOL', 'stringeol_style'), |
|---|
| 73 | ('STC_P_TRIPLE', 'string_style'), |
|---|
| 74 | ('STC_P_TRIPLEDOUBLE', 'string_style'), |
|---|
| 75 | ('STC_P_WORD', 'keyword_style'), |
|---|
| 76 | ('STC_P_WORD2', 'userkw_style')] |
|---|
| 77 | |
|---|
| 78 | #---- Extra Properties ----# |
|---|
| 79 | FOLD = ("fold", "1") |
|---|
| 80 | TIMMY = ("tab.timmy.whinge.level", "1") # Mark Inconsistant indentation |
|---|
| 81 | |
|---|
| 82 | #-----------------------------------------------------------------------------# |
|---|
| 83 | |
|---|
| 84 | #---- Required Module Functions ----# |
|---|
| 85 | def Keywords(lang_id=0): |
|---|
| 86 | """Returns Specified Keywords List |
|---|
| 87 | @param lang_id: used to select specific subset of keywords |
|---|
| 88 | |
|---|
| 89 | """ |
|---|
| 90 | return [PY_KW, PY_BIN] |
|---|
| 91 | |
|---|
| 92 | def SyntaxSpec(lang_id=0): |
|---|
| 93 | """Syntax Specifications |
|---|
| 94 | @param lang_id: used for selecting a specific subset of syntax specs |
|---|
| 95 | |
|---|
| 96 | """ |
|---|
| 97 | return SYNTAX_ITEMS |
|---|
| 98 | |
|---|
| 99 | def Properties(lang_id=0): |
|---|
| 100 | """Returns a list of Extra Properties to set |
|---|
| 101 | @param lang_id: used to select a specific set of properties |
|---|
| 102 | |
|---|
| 103 | """ |
|---|
| 104 | return [FOLD, TIMMY] |
|---|
| 105 | |
|---|
| 106 | def CommentPattern(lang_id=0): |
|---|
| 107 | """Returns a list of characters used to comment a block of code |
|---|
| 108 | @param lang_id: used to select a specific subset of comment pattern(s) |
|---|
| 109 | |
|---|
| 110 | """ |
|---|
| 111 | return [u'#'] |
|---|
| 112 | |
|---|
| 113 | def AutoIndenter(stc, pos, ichar): |
|---|
| 114 | """Auto indent python code. uses \n the text buffer will |
|---|
| 115 | handle any eol character formatting. |
|---|
| 116 | @param stc: EditraStyledTextCtrl |
|---|
| 117 | @param pos: current carat position |
|---|
| 118 | @param ichar: Indentation character |
|---|
| 119 | @return: string |
|---|
| 120 | |
|---|
| 121 | """ |
|---|
| 122 | rtxt = u'' |
|---|
| 123 | line = stc.GetCurrentLine() |
|---|
| 124 | spos = stc.PositionFromLine(line) |
|---|
| 125 | text = stc.GetTextRange(spos, pos) |
|---|
| 126 | epos = stc.GetLineEndPosition(line) |
|---|
| 127 | inspace = text.isspace() |
|---|
| 128 | |
|---|
| 129 | # Cursor is in the indent area somewhere |
|---|
| 130 | if inspace: |
|---|
| 131 | return u"\n" + text |
|---|
| 132 | |
|---|
| 133 | # Check if the cursor is in column 0 and just return newline. |
|---|
| 134 | if not len(text): |
|---|
| 135 | return u"\n" |
|---|
| 136 | |
|---|
| 137 | # Ignore empty lines and backtrace to find the previous line that we can |
|---|
| 138 | # get the indent position from |
|---|
| 139 | # while text.isspace(): |
|---|
| 140 | # line -= 1 |
|---|
| 141 | # if line < 0: |
|---|
| 142 | # return u'' |
|---|
| 143 | # text = stc.GetTextRange(stc.PositionFromLine(line), pos) |
|---|
| 144 | |
|---|
| 145 | indent = stc.GetLineIndentation(line) |
|---|
| 146 | # if ichar == u"\t": |
|---|
| 147 | # tabw = stc.GetTabWidth() |
|---|
| 148 | # else: |
|---|
| 149 | # tabw = stc.GetIndent() |
|---|
| 150 | |
|---|
| 151 | tabw = 4; # hardcode for Cobra |
|---|
| 152 | i_space = indent / tabw |
|---|
| 153 | end_spaces = ((indent - (tabw * i_space)) * u" ") |
|---|
| 154 | |
|---|
| 155 | tokens = filter(None, text.strip().split()) |
|---|
| 156 | if tokens and not inspace: |
|---|
| 157 | if tokens[-1].endswith(u""): |
|---|
| 158 | if tokens[0] in INDENT_KW: |
|---|
| 159 | i_space += 1 |
|---|
| 160 | elif tokens[0] in UNINDENT_KW: |
|---|
| 161 | i_space = max(i_space - 1, 0) |
|---|
| 162 | elif tokens[-1].endswith(u"\\"): |
|---|
| 163 | i_space += 1 |
|---|
| 164 | |
|---|
| 165 | rval = u"\n" + (ichar * i_space) + end_spaces |
|---|
| 166 | if inspace and ichar != u"\t": |
|---|
| 167 | rpos = indent - (pos - spos) |
|---|
| 168 | if rpos < len(rval) and rpos > 0: |
|---|
| 169 | rval = rval[:-rpos] |
|---|
| 170 | elif rpos >= len(rval): |
|---|
| 171 | rval = u"\n" |
|---|
| 172 | |
|---|
| 173 | return rval |
|---|
| 174 | |
|---|
| 175 | #---- End Required Module Functions ----# |
|---|
| 176 | |
|---|
| 177 | #---- Syntax Modules Internal Functions ----# |
|---|
| 178 | def KeywordString(): |
|---|
| 179 | """Returns the specified Keyword String |
|---|
| 180 | @note: not used by most modules |
|---|
| 181 | |
|---|
| 182 | """ |
|---|
| 183 | return PY_KW[1] |
|---|
| 184 | |
|---|
| 185 | #---- End Syntax Modules Internal Functions ----# |
|---|
| 186 | |
|---|
| 187 | #-----------------------------------------------------------------------------# |
|---|