demo_javascript.pp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. program test_javascript;
  2. {$mode objfpc}{$H+}
  3. uses
  4. SysUtils, Classes, syntax.highlighter, syntax.javascript;
  5. procedure TestJavaScriptKeywords;
  6. const
  7. JSKeywords: array[0..19] of string = (
  8. 'var', 'let', 'const', 'function', 'if', 'else', 'for', 'while', 'do', 'switch',
  9. 'case', 'default', 'break', 'continue', 'return', 'try', 'catch', 'finally', 'throw', 'new'
  10. );
  11. var
  12. highlighter: TJavaScriptSyntaxHighlighter;
  13. tokens: TSyntaxTokenArray;
  14. i: Integer;
  15. begin
  16. WriteLn('Testing JavaScript Keywords:');
  17. WriteLn('===========================');
  18. highlighter := TJavaScriptSyntaxHighlighter.Create;
  19. try
  20. for i := 0 to High(JSKeywords) do begin
  21. tokens := highlighter.Execute(JSKeywords[i]);
  22. if (Length(tokens) = 1) and (tokens[0].Kind = shKeyword) then
  23. WriteLn(JSKeywords[i] + ': PASS - recognized as keyword')
  24. else
  25. WriteLn(JSKeywords[i] + ': FAIL - kind=' + IntToStr(Ord(tokens[0].Kind)));
  26. end;
  27. finally
  28. highlighter.Free;
  29. end;
  30. WriteLn;
  31. end;
  32. procedure TestJavaScriptTokenTypes;
  33. var
  34. highlighter: TJavaScriptSyntaxHighlighter;
  35. tokens: TSyntaxTokenArray;
  36. begin
  37. WriteLn('Testing JavaScript Token Types:');
  38. WriteLn('==============================');
  39. highlighter := TJavaScriptSyntaxHighlighter.Create;
  40. try
  41. // Test single-quoted string
  42. tokens := highlighter.Execute('''hello world''');
  43. if (Length(tokens) = 1) and (tokens[0].Kind = shStrings) then
  44. WriteLn('Single-quoted string: PASS')
  45. else
  46. WriteLn('Single-quoted string: FAIL - kind=' + IntToStr(Ord(tokens[0].Kind)));
  47. // Test double-quoted string
  48. tokens := highlighter.Execute('"hello world"');
  49. if (Length(tokens) = 1) and (tokens[0].Kind = shStrings) then
  50. WriteLn('Double-quoted string: PASS')
  51. else
  52. WriteLn('Double-quoted string: FAIL');
  53. // Test template literal
  54. tokens := highlighter.Execute('`hello ${name}`');
  55. if (Length(tokens) = 1) and (tokens[0].Kind = shRawString) then
  56. WriteLn('Template literal: PASS')
  57. else
  58. WriteLn('Template literal: FAIL - kind=' + IntToStr(Ord(tokens[0].Kind)));
  59. // Test regex literal
  60. tokens := highlighter.Execute('/[a-z]+/gi');
  61. if (Length(tokens) = 1) and (tokens[0].Kind = shRegex) then
  62. WriteLn('Regex literal: PASS')
  63. else
  64. WriteLn('Regex literal: FAIL - kind=' + IntToStr(Ord(tokens[0].Kind)));
  65. // Test single-line comment
  66. tokens := highlighter.Execute('// This is a comment');
  67. if (Length(tokens) = 1) and (tokens[0].Kind = shComment) then
  68. WriteLn('Single-line comment: PASS')
  69. else
  70. WriteLn('Single-line comment: FAIL');
  71. // Test multi-line comment
  72. tokens := highlighter.Execute('/* This is a comment */');
  73. if (Length(tokens) = 1) and (tokens[0].Kind = shComment) then
  74. WriteLn('Multi-line comment: PASS')
  75. else
  76. WriteLn('Multi-line comment: FAIL');
  77. // Test number
  78. tokens := highlighter.Execute('123.45');
  79. if (Length(tokens) = 1) and (tokens[0].Kind = shNumbers) then
  80. WriteLn('Decimal number: PASS')
  81. else
  82. WriteLn('Decimal number: FAIL');
  83. // Test hex number
  84. tokens := highlighter.Execute('0xFF');
  85. if (Length(tokens) = 1) and (tokens[0].Kind = shNumbers) then
  86. WriteLn('Hex number: PASS')
  87. else
  88. WriteLn('Hex number: FAIL');
  89. // Test operator
  90. tokens := highlighter.Execute('===');
  91. if (Length(tokens) = 1) and (tokens[0].Kind = shOperator) then
  92. WriteLn('Operator: PASS')
  93. else
  94. WriteLn('Operator: FAIL');
  95. // Test identifier
  96. tokens := highlighter.Execute('myVariable');
  97. if (Length(tokens) = 1) and (tokens[0].Kind = shDefault) then
  98. WriteLn('Identifier: PASS')
  99. else
  100. WriteLn('Identifier: FAIL');
  101. finally
  102. highlighter.Free;
  103. end;
  104. WriteLn;
  105. end;
  106. procedure TestJavaScriptNumbers;
  107. var
  108. highlighter: TJavaScriptSyntaxHighlighter;
  109. tokens: TSyntaxTokenArray;
  110. begin
  111. WriteLn('Testing JavaScript Number Formats:');
  112. WriteLn('==================================');
  113. highlighter := TJavaScriptSyntaxHighlighter.Create;
  114. try
  115. // Test scientific notation
  116. tokens := highlighter.Execute('1.23e-4');
  117. if (Length(tokens) = 1) and (tokens[0].Kind = shNumbers) then
  118. WriteLn('Scientific notation: PASS')
  119. else
  120. WriteLn('Scientific notation: FAIL');
  121. // Test binary number
  122. tokens := highlighter.Execute('0b1010');
  123. if (Length(tokens) = 1) and (tokens[0].Kind = shNumbers) then
  124. WriteLn('Binary number: PASS')
  125. else
  126. WriteLn('Binary number: FAIL');
  127. finally
  128. highlighter.Free;
  129. end;
  130. WriteLn;
  131. end;
  132. procedure TestJavaScriptFunction;
  133. var
  134. highlighter: TJavaScriptSyntaxHighlighter;
  135. tokens: TSyntaxTokenArray;
  136. i: Integer;
  137. jsCode: string;
  138. begin
  139. WriteLn('Testing Complete JavaScript Function:');
  140. WriteLn('====================================');
  141. jsCode := '/* This is a comment */';
  142. highlighter := TJavaScriptSyntaxHighlighter.Create;
  143. try
  144. tokens := highlighter.Execute(jsCode);
  145. WriteLn('Code: ', jsCode);
  146. WriteLn('Tokens (', Length(tokens), '):');
  147. for i := 0 to High(tokens) do begin
  148. if Trim(tokens[i].Text) <> '' then
  149. WriteLn(' "', tokens[i].Text, '" - Kind: ', Ord(tokens[i].Kind),
  150. ' (', tokens[i].Kind, ') - Category: ', tokens[i].CategoriesAsString);
  151. end;
  152. finally
  153. highlighter.Free;
  154. end;
  155. WriteLn;
  156. end;
  157. procedure TestRegexContext;
  158. var
  159. highlighter: TJavaScriptSyntaxHighlighter;
  160. tokens: TSyntaxTokenArray;
  161. begin
  162. WriteLn('Testing Regex vs Division Context:');
  163. WriteLn('==================================');
  164. highlighter := TJavaScriptSyntaxHighlighter.Create;
  165. try
  166. // This should be recognized as regex (after =)
  167. tokens := highlighter.Execute('var pattern = /test/;');
  168. WriteLn('Code: var pattern = /test/;');
  169. if (Length(tokens) >= 5) and (tokens[4].Kind = shRegex) then
  170. WriteLn(' Regex after = : PASS')
  171. else
  172. WriteLn(' Regex after = : FAIL');
  173. // This should be division (after variable)
  174. tokens := highlighter.Execute('result = a / b;');
  175. WriteLn('Code: result = a / b;');
  176. if (Length(tokens) >= 5) and (tokens[4].Kind = shOperator) then
  177. WriteLn(' Division: PASS')
  178. else
  179. WriteLn(' Division: FAIL - kind=' + IntToStr(Ord(tokens[4].Kind)));
  180. finally
  181. highlighter.Free;
  182. end;
  183. WriteLn;
  184. end;
  185. procedure TestCategorySystem;
  186. var
  187. categories: TStringList;
  188. jsCategoryID: Integer;
  189. i: Integer;
  190. begin
  191. WriteLn('Testing Category System for JavaScript:');
  192. WriteLn('======================================');
  193. jsCategoryID := TSyntaxHighlighter.RegisterCategory('JavaScript');
  194. WriteLn('JavaScript category ID: ', jsCategoryID);
  195. categories := TStringList.Create;
  196. try
  197. TSyntaxHighlighter.GetRegisteredCategories(categories);
  198. WriteLn('All registered categories:');
  199. for i := 0 to categories.Count - 1 do
  200. WriteLn(' ', categories[i], ' = ', PtrInt(categories.Objects[i]));
  201. finally
  202. categories.Free;
  203. end;
  204. WriteLn;
  205. end;
  206. begin
  207. WriteLn('JavaScript Syntax Highlighter Test');
  208. WriteLn('===================================');
  209. WriteLn;
  210. TestCategorySystem;
  211. TestJavaScriptKeywords;
  212. TestJavaScriptTokenTypes;
  213. TestJavaScriptNumbers;
  214. TestRegexContext;
  215. TestJavaScriptFunction;
  216. WriteLn('Test completed. Press Enter to exit.');
  217. ReadLn;
  218. end.