44
55module CodeRay
66module Scanners
7-
7+
88class AppleScript <Scanner
99
1010register_for :applescript
11-
11+
1212RESERVED_WORDS = [
1313'#include' , 'for' , 'foreach' , 'if' , 'elseif' , 'else' , 'while' , 'do' , 'dowhile' , 'end' ,
1414'switch' , 'case' , 'return' , 'break' , 'continue' , 'in' , 'to' , 'of' , 'repeat' , 'tell' , 'then' , 'as'
@@ -100,9 +100,9 @@ class AppleScript < Scanner
100100'UTC' , 'valueOf' , 'variable' , 'version' , 'Video' , 'visible' , 'void' , 'watch' , 'width' ,
101101'with' , 'wordwrap' , 'XML' , 'xmlDecl' , 'XMLNode' , 'XMLSocket'
102102]
103-
103+
104104PREDEFINED_TYPES = [
105- 'boolean' , 'small integer' , 'integer' , 'double integer' ,
105+ 'boolean' , 'small integer' , 'integer' , 'double integer' ,
106106'small real' , 'real' , 'date' , 'list' , 'record' , 'string' , 'class'
107107]
108108
@@ -118,21 +118,21 @@ class AppleScript < Scanner
118118'all caps' , 'all lowercase' , 'bold' , 'condensed' , 'expanded' , 'hidden' , 'italic' , 'outline' , 'plain' , 'shadow' , 'small caps' , 'strikethrough' , 'subscript' , 'superscript' , 'underline' ,
119119'version'
120120]
121-
121+
122122PLAIN_STRING_CONTENT = {
123123"'" => /[^'\n ]+/ ,
124124'"' => /[^"\n ]+/ ,
125125}
126-
127-
126+
127+
128128IDENT_KIND = CaseIgnoringWordList . new ( :ident ) .
129129add ( RESERVED_WORDS , :reserved ) .
130130add ( KEYWORD_OPERATOR , :operator ) .
131131add ( DIRECTIVES , :directive ) .
132132add ( PREDEFINED_TYPES , :pre_type ) .
133133add ( PREDEFINED_CONSTANTS , :pre_constant )
134-
135-
134+
135+
136136
137137def scan_tokens tokens , options
138138
@@ -145,70 +145,70 @@ def scan_tokens tokens, options
145145match = nil
146146
147147if state ==:initial
148-
148+
149149if scan ( /\s +/x )
150150kind = :space
151-
151+
152152elsif scan ( %r!\{ \$ [^}]*\} ? |\( \* \$ (?: .*?\* \) | .* ) !mx )
153153kind = :preprocessor
154-
155- elsif scan ( /^[\s \t ]*--.*/x )
154+
155+ elsif scan ( /^[\s ]*--.*/x )
156156kind = :comment
157157elsif scan ( /\( \* (?: .*?\* \) $ | .* )/mx )
158158kind = :comment
159-
159+
160160elsif scan ( / [-+*\/ =<>:;,.@\^ |\( \) \[ \] ]+ /x )
161161kind = :operator
162-
162+
163163elsif match = scan ( / [A-Za-z_][A-Za-z_0-9]* /x )
164164kind = IDENT_KIND [ match ]
165-
165+
166166elsif match = scan ( / ' ( [^\n ']|'' ) (?:'|$) /x )
167167tokens <<[ :open , :char ]
168168tokens <<[ "'" , :delimiter ]
169169tokens <<[ self [ 1 ] , :content ]
170170tokens <<[ "'" , :delimiter ]
171171tokens <<[ :close , :char ]
172172next
173-
173+
174174elsif match = scan ( /["']/ )
175175tokens <<[ :open , :string ]
176176state = :string
177177plain_string_content = PLAIN_STRING_CONTENT [ match ]
178178kind = :delimiter
179-
179+
180180else
181181kind = :plain
182182getch
183183
184184end
185-
186- elsif state ==:string
187- if scan ( plain_string_content )
188- kind = :content
189- elsif scan ( /['"]/ )
190- tokens <<[ matched , :delimiter ]
191- tokens <<[ :close , :string ]
192- state = :initial
193- next
194- elsif scan ( /\\ | $ /x )
195- tokens <<[ :close , :string ]
196- kind = :error
197- state = :initial
198- end
199185
200- else
201- raise_inspect "else case\" reached; %p not handled." %peek ( 1 ) , tokens
186+ elsif state ==:string
187+ if scan ( plain_string_content )
188+ kind = :content
189+ elsif scan ( /['"]/ )
190+ tokens <<[ matched , :delimiter ]
191+ tokens <<[ :close , :string ]
192+ state = :initial
193+ next
194+ elsif scan ( /\\ | $ /x )
195+ tokens <<[ :close , :string ]
196+ kind = :error
197+ state = :initial
202198end
203-
199+
200+ else
201+ raise_inspect "else case\" reached; %p not handled." %peek ( 1 ) , tokens
202+ end
203+
204204match ||=matched
205205if $DEBUGand notkind
206206raise_inspect 'Error token %p in line %d' %
207207[ [ match , kind ] , line ] , tokens
208208end
209209raise_inspect 'Empty token' , tokens unless match
210210tokens <<[ match , kind ]
211-
211+
212212end
213213tokens
214214end