swift.test.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. 'use strict';
  6. import {testTokenization} from './testRunner';
  7. testTokenization('swift', [
  8. // Attributes
  9. [{
  10. line: '@noescape',
  11. tokens: [
  12. { startIndex: 0, type: 'keyword.control.swift' } /* '@noescape' */
  13. ]}],
  14. //Keyword and Type Identifier
  15. [{
  16. line: 'class App: UI, UIApp, UIView {',
  17. tokens: [
  18. { startIndex: 0, type: 'keyword.swift' } /* 'class' */,
  19. { startIndex: 5, type: '' },
  20. { startIndex: 6, type: 'type.identifier.swift' } /* 'App' */,
  21. { startIndex: 9, type: 'keyword.operator.swift' } /* ':' */,
  22. { startIndex: 10, type: '' },
  23. { startIndex: 11, type: 'type.identifier.swift' } /* 'UI' */,
  24. { startIndex: 13, type: 'keyword.operator.swift' } /* ',' */,
  25. { startIndex: 14, type: '' },
  26. { startIndex: 15, type: 'type.identifier.swift' } /* 'UIApp' */,
  27. { startIndex: 20, type: 'keyword.operator.swift' } /* ',' */,
  28. { startIndex: 21, type: '' },
  29. { startIndex: 22, type: 'type.identifier.swift' } /* 'UIView' */,
  30. { startIndex: 28, type: '' },
  31. { startIndex: 29, type: 'delimiter.curly.swift' } /* '{' */
  32. ]}],
  33. // Keyword, Identifier, and Type Identifier
  34. [{
  35. line: ' var window: UIWindow?',
  36. tokens: [
  37. { startIndex: 0, type: '' },
  38. { startIndex: 4, type: 'keyword.swift' } /* 'var' */,
  39. { startIndex: 7, type: '' },
  40. { startIndex: 8, type: 'identifier.swift' } /* 'window' */,
  41. { startIndex: 14, type: 'keyword.operator.swift' } /* ':' */,
  42. { startIndex: 15, type: '' },
  43. { startIndex: 16, type: 'type.identifier.swift' } /* 'UIWindow' */,
  44. { startIndex: 24, type: 'keyword.operator.swift' } /* '?' */
  45. ]}],
  46. //Comment
  47. [{
  48. line: ' // Comment',
  49. tokens: [
  50. { startIndex: 0, type: '' },
  51. { startIndex: 4, type: 'comment.swift' } /* '// Comment' */
  52. ]}],
  53. //Block Comment with Embedded Comment followed by code
  54. [{
  55. line: ' /* Comment //Embedded */ var y = 0b10',
  56. tokens: [
  57. { startIndex: 0, type: '' },
  58. { startIndex: 4, type: 'comment.swift' }, // /* '/* Comment //Embedded */' */,
  59. { startIndex: 28, type: '' },
  60. { startIndex: 29, type: 'keyword.swift' } /* 'var' */,
  61. { startIndex: 32, type: '' },
  62. { startIndex: 33, type: 'identifier.swift' } /* 'y' */,
  63. { startIndex: 34, type: '' },
  64. { startIndex: 35, type: 'keyword.operator.swift' } /* '=' */,
  65. { startIndex: 36, type: '' },
  66. { startIndex: 37, type: 'number.binary.swift' } /* '0b10' */
  67. ]}],
  68. // Method signature (broken on two lines)
  69. [{
  70. line: ' public func app(app: App, opts:',
  71. tokens:[
  72. { startIndex: 0, type: '' },
  73. { startIndex: 4, type: 'keyword.swift' } /* 'public' */,
  74. { startIndex: 10, type: '' },
  75. { startIndex: 11, type: 'keyword.swift' } /* 'func' */,
  76. { startIndex: 15, type: '' },
  77. { startIndex: 16, type: 'identifier.swift' } /* 'app' */,
  78. { startIndex: 19, type: 'delimiter.parenthesis.swift' } /* '(' */,
  79. { startIndex: 20, type: 'identifier.swift' }/* 'app' */,
  80. { startIndex: 23, type: 'keyword.operator.swift' } /* ':' */,
  81. { startIndex: 24, type: '' },
  82. { startIndex: 25, type: 'type.identifier.swift' } /* 'App' */,
  83. { startIndex: 28, type: 'keyword.operator.swift' } /* ',' */,
  84. { startIndex: 29, type: '' },
  85. { startIndex: 30, type: 'identifier.swift' } /* 'opts' */,
  86. { startIndex: 34, type: 'keyword.operator.swift' } /* ':' */,
  87. ]}],
  88. // Method signature Continued
  89. [{
  90. line: ' [NSObject: AnyObject]?) -> Bool {',
  91. tokens: [
  92. { startIndex: 0, type: '' },
  93. { startIndex: 8, type: 'delimiter.square.swift' } /* '[' */,
  94. { startIndex: 9, type: 'type.identifier.swift' } /* 'NSObject' */,
  95. { startIndex: 17, type: 'keyword.operator.swift' } /* ':' */,
  96. { startIndex: 18, type: '' },
  97. { startIndex: 19, type: 'type.identifier.swift' } /* 'AnyObject' */,
  98. { startIndex: 28, type: 'delimiter.square.swift' } /* ']' */,
  99. { startIndex: 29, type: 'keyword.operator.swift' } /* '?' */,
  100. { startIndex: 30, type: 'delimiter.parenthesis.swift' } /* ')' */,
  101. { startIndex: 31, type: '' },
  102. { startIndex: 32, type: 'keyword.operator.swift' } /* '->' */,
  103. { startIndex: 34, type: '' },
  104. { startIndex: 35, type: 'type.identifier.swift' } /* 'Bool' */,
  105. { startIndex: 39, type: '' },
  106. { startIndex: 40, type: 'delimiter.curly.swift' } /* '{' */
  107. ]}],
  108. // String with escapes
  109. [{
  110. line: ' var `String` = "String w/ \\"escape\\""',
  111. tokens: [
  112. { startIndex: 0, type: '' },
  113. { startIndex: 8, type: 'keyword.swift' } /* 'var' */,
  114. { startIndex: 11, type: '' },
  115. { startIndex: 12, type: 'keyword.operator.swift' } /* '`' */,
  116. { startIndex: 13, type: 'identifier.swift' } /* 'String' */,
  117. { startIndex: 19, type: 'keyword.operator.swift' } /* '`' */,
  118. { startIndex: 20, type: '' },
  119. { startIndex: 21, type: 'keyword.operator.swift' } /* '=' */,
  120. { startIndex: 22, type: '' },
  121. { startIndex: 23, type: 'string.quote.swift' } /* '"' */,
  122. { startIndex: 24, type: 'string.swift' } /* 'String w/ \\"escape\\""' */,
  123. { startIndex: 44, type: 'string.quote.swift' } /* '"' */,
  124. ]}],
  125. // String with interpolated expression
  126. [{
  127. line: ' let message = "\\(y) times 2.5 is \\(Double(25) * 2.5)"',
  128. tokens: [
  129. { startIndex: 0, type: '' },
  130. { startIndex: 8, type: 'keyword.swift' } /* 'let' */,
  131. { startIndex: 11, type: '' },
  132. { startIndex: 12, type: 'identifier.swift' } /* 'message' */,
  133. { startIndex: 19, type: '' },
  134. { startIndex: 20, type: 'keyword.operator.swift' } /* '=' */,
  135. { startIndex: 21, type: '' },
  136. { startIndex: 22, type: 'string.quote.swift' } /* '"' */,
  137. { startIndex: 23, type: 'keyword.operator.swift' } /* '\(' */,
  138. { startIndex: 25, type: 'identifier.swift' },
  139. { startIndex: 26, type: 'keyword.operator.swift' } /* ')' */,
  140. { startIndex: 27, type: 'string.swift' } /* ' times 2.5 is ' */,
  141. { startIndex: 41, type: 'keyword.operator.swift' } /* '\(' */,
  142. { startIndex: 43, type: 'type.identifier.swift' } /* 'Double' */,
  143. { startIndex: 49, type: 'keyword.operator.swift' } /* '(' */,
  144. { startIndex: 50, type: 'number.swift' } /* '25' */,
  145. { startIndex: 52, type: 'keyword.operator.swift' } /* ')' */,
  146. { startIndex: 53, type: '' },
  147. { startIndex: 54, type: 'keyword.operator.swift' } /* '*' */,
  148. { startIndex: 55, type: '' },
  149. { startIndex: 56, type: 'number.float.swift' } /* '2.5' */,
  150. { startIndex: 59, type: 'keyword.operator.swift' } /* ')' */,
  151. { startIndex: 60, type: 'string.quote.swift' } /* '"' */
  152. ]}],
  153. // Method invocation/property accessor.
  154. [{
  155. line: ' let view = self.window!.contr as! UIView',
  156. tokens: [
  157. { startIndex: 0, type: '' },
  158. { startIndex: 8, type: 'keyword.swift' } /* 'let' */,
  159. { startIndex: 11, type: '' },
  160. { startIndex: 12, type: 'identifier.swift' } /* 'view' */,
  161. { startIndex: 16, type: '' },
  162. { startIndex: 17, type: 'keyword.operator.swift' } /* '=' */,
  163. { startIndex: 18, type: '' },
  164. { startIndex: 19, type: 'keyword.swift' } /* 'self' */,
  165. { startIndex: 23, type: 'delimeter.swift' } /* '.' */,
  166. { startIndex: 24, type: 'type.identifier.swift' } /* 'window' */,
  167. { startIndex: 30, type: 'keyword.operator.swift' } /* '!' */,
  168. { startIndex: 31, type: 'delimeter.swift' } /* '.' */,
  169. { startIndex: 32, type: 'type.identifier.swift' } /* 'contr' */,
  170. { startIndex: 37, type: '' },
  171. { startIndex: 38, type: 'keyword.swift' } /* 'as' */,
  172. { startIndex: 40, type: 'keyword.operator.swift' } /* '!' */,
  173. { startIndex: 41, type: '' },
  174. { startIndex: 42, type: 'type.identifier.swift' } /* 'UIView' */
  175. ]}]
  176. ]);