_nonmem.py
7.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
###############################################################################
# Name: nonmem.py #
# Purpose: Define NONMEM syntax for highlighting and other features #
# Author: Robert McLeay <robert\@fearthecow.net> #
# Copyright: (c) 2008 Cody Precord (staff\@editra.org) #
# (c) 2008 Torsten Mohr (none_yet) #
# (c) 2010 Robert McLeay (robert\@fearthecow.net) #
# License: wxWindows License #
###############################################################################
"""
FILE: nonmem.py
AUTHOR: Cody Precord, Torsten Mohr, Robert McLeay
@summary: Lexer configuration module for NONMEM control streams.
"""
__author__ = "Cody Precord <cprecord>, Torsten Mohr <none_yet>"
__svnid__ = "$Id: _nonmem.py 70229 2012-01-01 01:27:10Z CJP $"
__revision__ = "$Revision: 70229 $"
#-----------------------------------------------------------------------------#
# Imports
import wx.stc as stc
from pygments.lexer import RegexLexer, include, bygroups
from pygments.token import Token, Text, Comment, Operator, \
Keyword, Name, String, Number, Punctuation
import re
#Local Imports
import synglob
import syndata
#-----------------------------------------------------------------------------#
# Style Id's
# Style Id's
STC_NONMEM_DEFAULT, \
STC_NONMEM_COMMENT, \
STC_NONMEM_NUMBER, \
STC_NONMEM_STRING, \
STC_NONMEM_STRINGEOL, \
STC_NONMEM_OPERATOR, \
STC_NONMEM_NAME, \
STC_NONMEM_ABSTRACTRULE, \
STC_NONMEM_FEATURE, \
STC_NONMEM_CROSSREF, \
STC_NONMEM_PACKAGE, \
STC_NONMEM_KEYWORD, \
STC_NONMEM_KEYWORD_PSEUDO = range(13)
#-----------------------------------------------------------------------------#
#---- Keyword Specifications ----#
# Xtext Keywords
KEYWORDS = ("grammar generate import returns enum terminal hidden with as current")
TERMINALS = ("ID INT STRING")
#---- Syntax Style Specs ----#
SYNTAX_ITEMS = [ (STC_NONMEM_DEFAULT, 'default_style'),
(STC_NONMEM_COMMENT, 'comment_style'),
(STC_NONMEM_NUMBER, 'number_style'),
(STC_NONMEM_STRING, 'string_style'),
(STC_NONMEM_STRINGEOL, 'stringeol_style'),
(STC_NONMEM_OPERATOR, 'operator_style'),
(STC_NONMEM_NAME, 'default_style'),
(STC_NONMEM_ABSTRACTRULE, 'keyword3_style'),
(STC_NONMEM_FEATURE, 'default_style'),
(STC_NONMEM_CROSSREF, 'class_style'),
(STC_NONMEM_PACKAGE, 'class_style'),
(STC_NONMEM_KEYWORD, 'keyword_style'),
(STC_NONMEM_KEYWORD_PSEUDO, 'keyword2_style'), ]
NONMEM_KEYWORDS = ("ADVAN\d+ BLOCK COMP COND CONDITIONAL DEFDOSE DEFOBS "
"DOWHILE ELSE ENDDO ENDIF EXP FILE FIX FIXED ICALL IF "
"IGNORE INTER INTERACTION LOG MATRIX MAX MAXEVAL METHOD "
"NEWIND NOABORT NOAPPEND NOPRINT NOHEADER ONEHEADER PRINT "
"SIG SIGDIGITS SLOW SUBPROBLEMS THEN TOL TRANS1 TRANS2 "
"TRANS3 TRANS4 ONLYSIM ENDIF")
NONMEM_PARAMS = "DADT ERR EPS ETA THETA"
#NONMEM_SPECIAL = "\$COV $DATA $DES $ERROR $EST \$INPUT $MODEL $OMEGA $PRED \\$PK $PROB $PROBLEM $SIGMA $SIM $SUB $TABLE $THETA"
#-----------------------------------------------------------------------------#
class SyntaxData(syndata.SyntaxDataBase):
"""SyntaxData object for IssueLists
This class is primarily intended as an example to creating a custom
lexer.
"""
def __init__(self, langid):
super(SyntaxData, self).__init__(langid)
# Setup
self.SetLexer(stc.STC_LEX_CONTAINER)
self.RegisterFeature(synglob.FEATURE_STYLETEXT, StyleText)
def GetSyntaxSpec(self):
"""Syntax Specifications """
return SYNTAX_ITEMS
#---- End Required Module Functions ----#
def StyleText(_stc, start, end):
"""Style the text
@param _stc: Styled text control instance
@param start: Start position
@param end: end position
"""
for index, token, txt in lexer.get_tokens_unprocessed(_stc.GetTextRange(0, end)):
# print index, token, txt
style = TOKEN_MAP.get(token, STC_NONMEM_DEFAULT)
# print "Text=%s, len=%s" % (txt, len(txt))
_stc.StartStyling(index, 0x1f)
tlen = len(txt)
if tlen:
_stc.SetStyling(len(txt), style)
TOKEN_MAP = { Token.String : STC_NONMEM_STRING,
Token.Comment.Multiline : STC_NONMEM_COMMENT,
Token.Comment.Single : STC_NONMEM_COMMENT,
Token.Operator : STC_NONMEM_OPERATOR,
Token.Punctuation : STC_NONMEM_OPERATOR,
Token.Number.Integer : STC_NONMEM_NUMBER,
Token.Keyword : STC_NONMEM_KEYWORD,
Token.Keyword.Pseudo: STC_NONMEM_KEYWORD_PSEUDO,
Token.Name : STC_NONMEM_NAME,
Token.Name.AbstractRule : STC_NONMEM_ABSTRACTRULE,
Token.Name.Feature : STC_NONMEM_FEATURE,
Token.Name.CrossRef : STC_NONMEM_CROSSREF,
Token.Name.Package : STC_NONMEM_PACKAGE,
Token.Name.Package.EMF : STC_NONMEM_PACKAGE}
class NONMEMLexer(RegexLexer):
"""
Nonmem lexer based on statefull RegexLexer from pygments library.
"""
name = 'NONMEM'
aliases = ['nonmem']
filenames = ['*.ctl']
mimetypes = ['text/x-nonmem']
flags = re.MULTILINE | re.DOTALL # | re.UNICODE
#: optional Comment or Whitespace
#_ws = r'(?:\s|//.*?\n|/[*].*?[*]/)+'
def AltWords(words):
"""Makes lexer rule for alternative words from the given words list.
@param words: string consisting of space separated words
@return: string in the form \\bword1\\b|\\bword2\\b|\\bword3\\b...
"""
return "|".join([ "\\b%s\\b" % w for w in words.split()])
_ident = r'\^?[a-zA-Z_\$][a-zA-Z0-9_]*'
tokens = {
'root': [
(include('first')),
(_ident + r'(\.' + _ident + r')+', Name.Package),
('(' + _ident + r')(\s*)(returns)',
bygroups(Name.AbstractRule, Text.Whitespace, Keyword), 'parserrule'),
('(' + _ident + r')(\s*)(:)',
bygroups(Name.AbstractRule, Text.Whitespace, Punctuation), 'parserrule'),
(_ident, Name),
],
'first': [
(r';[^\n]*$', Comment.Single),
(r'\$[A-Z]+', Name.Package),
(r'[ \t]+', Text.Whitespace),
(r'"(\\\\|\\"|[^"])*"', String),
(r"'(\\\\|\\'|[^'])*'", String),
(r'\*|\?|\+|!|\||=|\?=|\+=|\.\.|->', Operator),
(r'[()\[\]{}:]', Punctuation),
(r'[0-9]+', Number.Integer),
(AltWords(NONMEM_KEYWORDS), Keyword),
(AltWords(NONMEM_PARAMS), Keyword.Pseudo),
# (AltWords(NONMEM_SPECIAL), Name.Package),
],
'parserrule': [
(include('first')),
('(' + _ident + r'(\.' + _ident + r')?)([ \t]*)(=|\?=|\+=)',
bygroups(Name.Feature, Text.Whitespace, Operator)),
(_ident + r'(\.' + _ident + r')+', Name.Package),
(_ident, Name.CrossRef),
],
}
lexer = NONMEMLexer()
if __name__=='__main__':
import codecs, sys
ftext = codecs.open(sys.argv[1], "r", "utf-8")
text = ftext.read()
ftext.close()
line=1
for index, token, txt in lexer.get_tokens_unprocessed(text):
if token is Token.EndOfLine:
line += 1
print line, token, txt