TextCompiler.Mod 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. MODULE TextCompiler; (** AUTHOR ""; PURPOSE ""; *)
  2. IMPORT Streams, Modules, Basic := FoxBasic, Compiler, TextUtilities, Diagnostics, Texts, Backend := FoxBackend, SyntaxTree := FoxSyntaxTree,
  3. CompilerInterface, Formats := FoxFormats, Strings, UTF8Strings, Commands;
  4. CONST
  5. Name = "Fox";
  6. Description = "Oberon Compiler";
  7. FileExtension = "MOD"; (*! temporary *)
  8. PROCEDURE GetClipboardReader(): Streams.Reader;
  9. VAR size : LONGINT;
  10. selectionText: Texts.Text;
  11. pcStr: POINTER TO ARRAY OF CHAR;
  12. stringReader: Streams.StringReader;
  13. BEGIN
  14. selectionText := Texts.clipboard;
  15. selectionText.AcquireRead;
  16. size := UTF8Size(selectionText,0,selectionText.GetLength())+1;
  17. NEW(pcStr,size);
  18. TextUtilities.SubTextToStr(selectionText,0,size,pcStr^);
  19. selectionText.ReleaseRead;
  20. NEW(stringReader,size);
  21. stringReader.Set(pcStr^);
  22. RETURN stringReader;
  23. END GetClipboardReader;
  24. PROCEDURE GetSelectionReader(): Streams.Reader;
  25. VAR a, b, size : LONGINT;
  26. selectionText: Texts.Text;
  27. from, to: Texts.TextPosition;
  28. pcStr: POINTER TO ARRAY OF CHAR;
  29. stringReader: Streams.StringReader;
  30. BEGIN
  31. IF Texts.GetLastSelection(selectionText, from, to) THEN
  32. selectionText.AcquireRead;
  33. a := MIN(from.GetPosition(), to.GetPosition());
  34. b := MAX(from.GetPosition(), to.GetPosition());
  35. size := UTF8Size(selectionText,a,b-a+1)+1;
  36. NEW(pcStr,size);
  37. TextUtilities.SubTextToStr(selectionText, a, b - a+1, pcStr^);
  38. selectionText.ReleaseRead;
  39. NEW(stringReader,b-a+1);
  40. stringReader.Set(pcStr^);
  41. ELSE
  42. stringReader := NIL;
  43. END;
  44. RETURN stringReader;
  45. END GetSelectionReader;
  46. PROCEDURE GetTextReader(text: Texts.Text; position: LONGINT): Streams.Reader;
  47. VAR size : LONGINT;
  48. pcStr: POINTER TO ARRAY OF CHAR;
  49. stringReader: Streams.StringReader;
  50. BEGIN
  51. text.AcquireRead;
  52. size := UTF8Size(text,position,text.GetLength())+1;
  53. NEW(pcStr,size);
  54. TextUtilities.SubTextToStr(text,position,size,pcStr^);
  55. text.ReleaseRead;
  56. NEW(stringReader,size);
  57. stringReader.Set(pcStr^);
  58. RETURN stringReader;
  59. END GetTextReader;
  60. PROCEDURE UTF8Size(text : Texts.Text; startPos, len : LONGINT): LONGINT;
  61. VAR i, length, pos,size : LONGINT; r : Texts.TextReader; ch : Texts.Char32; ok : BOOLEAN;
  62. string: ARRAY 16 OF CHAR;
  63. BEGIN
  64. text.AcquireRead;
  65. NEW(r, text);
  66. r.SetPosition(startPos);
  67. size := 0;i := 0; length := len; ok := TRUE;
  68. WHILE (i < length) & ok DO
  69. r.ReadCh(ch);
  70. IF (ch > 0) THEN
  71. pos := 0;
  72. ok := UTF8Strings.EncodeChar(ch, string, pos);
  73. INC(size,pos);
  74. END;
  75. INC(i);
  76. END;
  77. text.ReleaseRead;
  78. RETURN size
  79. END UTF8Size;
  80. PROCEDURE CompileText*(t: Texts.Text; CONST source: ARRAY OF CHAR; pos: LONGINT; CONST pc,opt: ARRAY OF CHAR; log: Streams.Writer;
  81. diagnostics : Diagnostics.Diagnostics; VAR error: BOOLEAN);
  82. VAR stringReader: Streams.StringReader;
  83. reader: Streams.Reader;
  84. options: Compiler.CompilerOptions;
  85. importCache: SyntaxTree.ModuleScope;
  86. BEGIN
  87. IF t = NIL THEN
  88. log.String ("No text available"); log.Ln; log.Update;
  89. error := TRUE; RETURN
  90. END;
  91. NEW(stringReader,LEN(opt));
  92. stringReader.Set(opt);
  93. IF Compiler.GetOptions(stringReader,log,diagnostics,options) THEN
  94. reader := GetTextReader(t,pos);
  95. IF pc # "" THEN INCL(options.flags,Compiler.FindPC); COPY(pc, options.findPC);
  96. ELSIF options.findPC # "" THEN INCL(options.flags,Compiler.FindPC)
  97. END;
  98. error := ~Compiler.Modules(source,reader,0,diagnostics, log, options, importCache)
  99. END;
  100. END CompileText;
  101. PROCEDURE CompileSelection*(context: Commands.Context);
  102. BEGIN
  103. Compiler.CompileReader(context,GetSelectionReader())
  104. END CompileSelection;
  105. PROCEDURE CompileClipboard*(context: Commands.Context);
  106. BEGIN
  107. Compiler.CompileReader(context,GetClipboardReader())
  108. END CompileClipboard;
  109. PROCEDURE Cleanup;
  110. BEGIN
  111. CompilerInterface.Unregister(Name);
  112. END Cleanup;
  113. BEGIN
  114. CompilerInterface.Register(Name, Description, FileExtension, CompileText);
  115. Modules.InstallTermHandler(Cleanup);
  116. END TextCompiler.