demo_simple.pp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. program test_simple;
  2. {$mode objfpc}{$H+}
  3. uses
  4. SysUtils, syntax.highlighter, syntax.pascal;
  5. procedure TestKeyword(const keyword: string);
  6. var
  7. tokens: TSyntaxTokenArray;
  8. highlighter: TSyntaxHighlighter;
  9. begin
  10. highlighter := TSyntaxHighlighter.Create;
  11. try
  12. tokens := highlighter.Execute(keyword);
  13. finally
  14. highlighter.Free;
  15. end;
  16. if Length(tokens) = 1 then begin
  17. if tokens[0].Kind = shKeyword then
  18. WriteLn(keyword + ': PASS - recognized as keyword')
  19. else
  20. WriteLn(keyword + ': FAIL - kind=' + IntToStr(Ord(tokens[0].Kind)));
  21. end else
  22. WriteLn(keyword + ': FAIL - token count=' + IntToStr(Length(tokens)));
  23. end;
  24. procedure TestAllKeywords;
  25. const
  26. Keywords: array[0..60] of string = (
  27. 'AND', 'ARRAY', 'ASM', 'ASSEMBLER',
  28. 'BEGIN', 'BREAK',
  29. 'CASE', 'CONST', 'CONSTRUCTOR', 'CLASS',
  30. 'DEFAULT', 'DESTRUCTOR', 'DIV', 'DO', 'DOWNTO',
  31. 'ELSE', 'END', 'EXCEPT', 'EXIT',
  32. 'FINALIZATION', 'FINALLY', 'FOR', 'FUNCTION',
  33. 'GOTO',
  34. 'IF', 'IMPLEMENTATION', 'IN', 'INHERITED', 'INITIALIZATION', 'INTERFACE',
  35. 'NIL', 'NOT',
  36. 'OBJECT', 'OF', 'ON', 'OR', 'OVERRIDE',
  37. 'PACKED', 'PRIVATE', 'PROCEDURE', 'PROGRAM', 'PROPERTY', 'PROTECTED',
  38. 'PUBLIC', 'PUBLISHED',
  39. 'RAISE', 'RECORD', 'REPEAT', 'RESOURCESTRING',
  40. 'SET',
  41. 'THEN', 'TRY', 'TYPE',
  42. 'UNIT', 'UNTIL', 'USES',
  43. 'VAR', 'VIRTUAL',
  44. 'WHILE', 'WITH',
  45. 'XOR'
  46. );
  47. var
  48. i: Integer;
  49. begin
  50. WriteLn('Testing all keywords:');
  51. WriteLn('====================');
  52. for i := 0 to High(Keywords) do
  53. TestKeyword(LowerCase(Keywords[i]));
  54. WriteLn;
  55. WriteLn('Testing other token types:');
  56. WriteLn('==========================');
  57. end;
  58. procedure TestOtherTokens;
  59. var
  60. tokens: TSyntaxTokenArray;
  61. highlighter: TSyntaxHighlighter;
  62. begin
  63. highlighter := TSyntaxHighlighter.Create;
  64. try
  65. // Test comment
  66. tokens := highlighter.Execute('{ comment }');
  67. if (Length(tokens) = 1) and (tokens[0].Kind = shComment) then
  68. WriteLn('Comment: PASS')
  69. else
  70. WriteLn('Comment: FAIL');
  71. // Test string
  72. tokens := highlighter.Execute('''hello''');
  73. if (Length(tokens) = 1) and (tokens[0].Kind = shStrings) then
  74. WriteLn('String: PASS')
  75. else
  76. WriteLn('String: FAIL - kind=' + IntToStr(Ord(tokens[0].Kind)));
  77. // Test character
  78. tokens := highlighter.Execute('''A''');
  79. if (Length(tokens) = 1) and (tokens[0].Kind = shCharacters) then
  80. WriteLn('Character: PASS')
  81. else
  82. WriteLn('Character: FAIL - kind=' + IntToStr(Ord(tokens[0].Kind)));
  83. // Test number
  84. tokens := highlighter.Execute('123');
  85. if (Length(tokens) = 1) and (tokens[0].Kind = shNumbers) then
  86. WriteLn('Number: PASS')
  87. else
  88. WriteLn('Number: FAIL');
  89. // Test hex number
  90. tokens := highlighter.Execute('$FF');
  91. if (Length(tokens) = 1) and (tokens[0].Kind = shNumbers) then
  92. WriteLn('Hex number: PASS')
  93. else
  94. WriteLn('Hex number: FAIL');
  95. // Test symbol
  96. tokens := highlighter.Execute(':=');
  97. if (Length(tokens) = 1) and (tokens[0].Kind = shSymbol) then
  98. WriteLn('Symbol: PASS')
  99. else
  100. WriteLn('Symbol: FAIL');
  101. // Test directive
  102. tokens := highlighter.Execute('{$MODE OBJFPC}');
  103. if (Length(tokens) = 1) and (tokens[0].Kind = shDirective) then
  104. WriteLn('Directive: PASS')
  105. else
  106. WriteLn('Directive: FAIL - kind=' + IntToStr(Ord(tokens[0].Kind)));
  107. // Test identifier
  108. tokens := highlighter.Execute('MyVariable');
  109. if (Length(tokens) = 1) and (tokens[0].Kind = shDefault) then
  110. WriteLn('Identifier: PASS')
  111. else
  112. WriteLn('Identifier: FAIL');
  113. finally
  114. highlighter.Free;
  115. end;
  116. end;
  117. begin
  118. WriteLn('Pascal Syntax Highlighter Test');
  119. WriteLn('==============================');
  120. WriteLn;
  121. TestAllKeywords;
  122. TestOtherTokens;
  123. WriteLn;
  124. WriteLn('Test completed. Press Enter to exit.');
  125. ReadLn;
  126. end.