Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

# -*- coding: utf-8 -*- 

""" 

    pygments.lexers.smalltalk 

    ~~~~~~~~~~~~~~~~~~~~~~~~~ 

 

    Lexers for Smalltalk and related languages. 

 

    :copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS. 

    :license: BSD, see LICENSE for details. 

""" 

 

from pygments.lexer import RegexLexer, include, bygroups, default 

from pygments.token import Text, Comment, Operator, Keyword, Name, String, \ 

    Number, Punctuation 

 

__all__ = ['SmalltalkLexer', 'NewspeakLexer'] 

 

 

class SmalltalkLexer(RegexLexer): 

    """ 

    For `Smalltalk <http://www.smalltalk.org/>`_ syntax. 

    Contributed by Stefan Matthias Aust. 

    Rewritten by Nils Winter. 

 

    .. versionadded:: 0.10 

    """ 

    name = 'Smalltalk' 

    filenames = ['*.st'] 

    aliases = ['smalltalk', 'squeak', 'st'] 

    mimetypes = ['text/x-smalltalk'] 

 

    tokens = { 

        'root': [ 

            (r'(<)(\w+:)(.*?)(>)', bygroups(Text, Keyword, Text, Text)), 

            include('squeak fileout'), 

            include('whitespaces'), 

            include('method definition'), 

            (r'(\|)([\w\s]*)(\|)', bygroups(Operator, Name.Variable, Operator)), 

            include('objects'), 

            (r'\^|\:=|\_', Operator), 

            # temporaries 

            (r'[\]({}.;!]', Text), 

        ], 

        'method definition': [ 

            # Not perfect can't allow whitespaces at the beginning and the 

            # without breaking everything 

            (r'([a-zA-Z]+\w*:)(\s*)(\w+)', 

             bygroups(Name.Function, Text, Name.Variable)), 

            (r'^(\b[a-zA-Z]+\w*\b)(\s*)$', bygroups(Name.Function, Text)), 

            (r'^([-+*/\\~<>=|&!?,@%]+)(\s*)(\w+)(\s*)$', 

             bygroups(Name.Function, Text, Name.Variable, Text)), 

        ], 

        'blockvariables': [ 

            include('whitespaces'), 

            (r'(:)(\s*)(\w+)', 

             bygroups(Operator, Text, Name.Variable)), 

            (r'\|', Operator, '#pop'), 

            default('#pop'),  # else pop 

        ], 

        'literals': [ 

            (r"'(''|[^'])*'", String, 'afterobject'), 

            (r'\$.', String.Char, 'afterobject'), 

            (r'#\(', String.Symbol, 'parenth'), 

            (r'\)', Text, 'afterobject'), 

            (r'(\d+r)?-?\d+(\.\d+)?(e-?\d+)?', Number, 'afterobject'), 

        ], 

        '_parenth_helper': [ 

            include('whitespaces'), 

            (r'(\d+r)?-?\d+(\.\d+)?(e-?\d+)?', Number), 

            (r'[-+*/\\~<>=|&#!?,@%\w:]+', String.Symbol), 

            # literals 

            (r"'(''|[^'])*'", String), 

            (r'\$.', String.Char), 

            (r'#*\(', String.Symbol, 'inner_parenth'), 

        ], 

        'parenth': [ 

            # This state is a bit tricky since 

            # we can't just pop this state 

            (r'\)', String.Symbol, ('root', 'afterobject')), 

            include('_parenth_helper'), 

        ], 

        'inner_parenth': [ 

            (r'\)', String.Symbol, '#pop'), 

            include('_parenth_helper'), 

        ], 

        'whitespaces': [ 

            # skip whitespace and comments 

            (r'\s+', Text), 

            (r'"(""|[^"])*"', Comment), 

        ], 

        'objects': [ 

            (r'\[', Text, 'blockvariables'), 

            (r'\]', Text, 'afterobject'), 

            (r'\b(self|super|true|false|nil|thisContext)\b', 

             Name.Builtin.Pseudo, 'afterobject'), 

            (r'\b[A-Z]\w*(?!:)\b', Name.Class, 'afterobject'), 

            (r'\b[a-z]\w*(?!:)\b', Name.Variable, 'afterobject'), 

            (r'#("(""|[^"])*"|[-+*/\\~<>=|&!?,@%]+|[\w:]+)', 

             String.Symbol, 'afterobject'), 

            include('literals'), 

        ], 

        'afterobject': [ 

            (r'! !$', Keyword, '#pop'),  # squeak chunk delimiter 

            include('whitespaces'), 

            (r'\b(ifTrue:|ifFalse:|whileTrue:|whileFalse:|timesRepeat:)', 

             Name.Builtin, '#pop'), 

            (r'\b(new\b(?!:))', Name.Builtin), 

            (r'\:=|\_', Operator, '#pop'), 

            (r'\b[a-zA-Z]+\w*:', Name.Function, '#pop'), 

            (r'\b[a-zA-Z]+\w*', Name.Function), 

            (r'\w+:?|[-+*/\\~<>=|&!?,@%]+', Name.Function, '#pop'), 

            (r'\.', Punctuation, '#pop'), 

            (r';', Punctuation), 

            (r'[\])}]', Text), 

            (r'[\[({]', Text, '#pop'), 

        ], 

        'squeak fileout': [ 

            # Squeak fileout format (optional) 

            (r'^"(""|[^"])*"!', Keyword), 

            (r"^'(''|[^'])*'!", Keyword), 

            (r'^(!)(\w+)( commentStamp: )(.*?)( prior: .*?!\n)(.*?)(!)', 

                bygroups(Keyword, Name.Class, Keyword, String, Keyword, Text, Keyword)), 

            (r"^(!)(\w+(?: class)?)( methodsFor: )('(?:''|[^'])*')(.*?!)", 

                bygroups(Keyword, Name.Class, Keyword, String, Keyword)), 

            (r'^(\w+)( subclass: )(#\w+)' 

             r'(\s+instanceVariableNames: )(.*?)' 

             r'(\s+classVariableNames: )(.*?)' 

             r'(\s+poolDictionaries: )(.*?)' 

             r'(\s+category: )(.*?)(!)', 

                bygroups(Name.Class, Keyword, String.Symbol, Keyword, String, Keyword, 

                         String, Keyword, String, Keyword, String, Keyword)), 

            (r'^(\w+(?: class)?)(\s+instanceVariableNames: )(.*?)(!)', 

                bygroups(Name.Class, Keyword, String, Keyword)), 

            (r'(!\n)(\].*)(! !)$', bygroups(Keyword, Text, Keyword)), 

            (r'! !$', Keyword), 

        ], 

    } 

 

 

class NewspeakLexer(RegexLexer): 

    """ 

    For `Newspeak <http://newspeaklanguage.org/>` syntax. 

 

    .. versionadded:: 1.1 

    """ 

    name = 'Newspeak' 

    filenames = ['*.ns2'] 

    aliases = ['newspeak', ] 

    mimetypes = ['text/x-newspeak'] 

 

    tokens = { 

        'root': [ 

            (r'\b(Newsqueak2)\b', Keyword.Declaration), 

            (r"'[^']*'", String), 

            (r'\b(class)(\s+)(\w+)(\s*)', 

             bygroups(Keyword.Declaration, Text, Name.Class, Text)), 

            (r'\b(mixin|self|super|private|public|protected|nil|true|false)\b', 

             Keyword), 

            (r'(\w+\:)(\s*)([a-zA-Z_]\w+)', 

             bygroups(Name.Function, Text, Name.Variable)), 

            (r'(\w+)(\s*)(=)', 

             bygroups(Name.Attribute, Text, Operator)), 

            (r'<\w+>', Comment.Special), 

            include('expressionstat'), 

            include('whitespace') 

        ], 

 

        'expressionstat': [ 

            (r'(\d+\.\d*|\.\d+|\d+[fF])[fF]?', Number.Float), 

            (r'\d+', Number.Integer), 

            (r':\w+', Name.Variable), 

            (r'(\w+)(::)', bygroups(Name.Variable, Operator)), 

            (r'\w+:', Name.Function), 

            (r'\w+', Name.Variable), 

            (r'\(|\)', Punctuation), 

            (r'\[|\]', Punctuation), 

            (r'\{|\}', Punctuation), 

 

            (r'(\^|\+|\/|~|\*|<|>|=|@|%|\||&|\?|!|,|-|:)', Operator), 

            (r'\.|;', Punctuation), 

            include('whitespace'), 

            include('literals'), 

        ], 

        'literals': [ 

            (r'\$.', String), 

            (r"'[^']*'", String), 

            (r"#'[^']*'", String.Symbol), 

            (r"#\w+:?", String.Symbol), 

            (r"#(\+|\/|~|\*|<|>|=|@|%|\||&|\?|!|,|-)+", String.Symbol) 

        ], 

        'whitespace': [ 

            (r'\s+', Text), 

            (r'"[^"]*"', Comment) 

        ], 

    }