home *** CD-ROM | disk | FTP | other *** search
/ Amiga Times / AmigaTimes.iso / programme / GoldED / developer / examples / syntax / example_simple / funcs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-06  |  8.0 KB  |  306 lines

  1. /* -----------------------------------------------------------------------------
  2.  
  3.  ScanLib ©1996 Dietmar Eilert
  4.  
  5.  GoldED syntax parser example library. Dice:
  6.  
  7.  DMAKE
  8.  
  9.  -------------------------------------------------------------------------------
  10.  
  11. */
  12.  
  13. #include "defs.h"
  14.  
  15. /// "Header stuff"
  16.  
  17. // Buffer handles are allocated for each text buffer to keep track of ressources:
  18.  
  19. struct BufferHandle {
  20.  
  21.     struct EditConfig     *bh_EditConfig;            // pointer to text data
  22.     struct GlobalConfig   *bh_GlobalConfig;          // editor configuration
  23.     struct SyntaxChunk    *bh_SyntaxStack;           // parser output
  24.     struct RefreshRequest  bh_RefreshRequest;        // display refresh request
  25. };
  26.  
  27. ///
  28. /// "Prototype"
  29.  
  30. // library functions
  31.  
  32. Prototype LibCall struct ParserData     *MountScanner(void);
  33. Prototype LibCall ULONG                  StartScanner(__A0 struct GlobalConfig *, __A1 struct EditConfig *, __D0 struct SyntaxChunk *);
  34. Prototype LibCall ULONG                  CloseScanner(__D0 ULONG);
  35. Prototype LibCall void                   FlushScanner(__D0 ULONG);
  36. Prototype LibCall void                   SetupScanner(__A0 struct GlobalConfig  *);
  37. Prototype LibCall struct RefreshRequest *BriefScanner(__D0 ULONG, __A0 struct ScannerNotify *);
  38. Prototype LibCall struct SyntaxChunk    *ParseLine   (__D0 ULONG, __A0 struct LineNode *, __D1 ULONG);
  39. Prototype LibCall void                   UnparseLines(__A0 struct LineNode *, __D0 ULONG);
  40. Prototype LibCall void                   ParseSection(__D0 ULONG, __A0 struct LineNode *, __D1 ULONG);
  41.  
  42. // private functions
  43.  
  44. Prototype struct SyntaxChunk *ParseString(UBYTE *, UWORD, ULONG);
  45.  
  46. ///
  47. /// "Library functions"
  48.  
  49. /* ------------------------------- MountScanner --------------------------------
  50.  
  51.  Called by the editor before first usage of a scanner. Return a description of
  52.  our abilities.
  53.  
  54. */
  55.  
  56. LibCall struct ParserData *
  57. MountScanner()
  58. {
  59.     static UBYTE version[] = "$VER: Comments 2.1 (" __COMMODORE_DATE__ ")";
  60.  
  61.     static struct ParserData parserData;
  62.  
  63.     // syntax elements understood by parser
  64.  
  65.     static UBYTE *levelNames[] = { "Standard text", "C++ comment ('//...')", NULL };
  66.  
  67.     static UBYTE *example[] = {
  68.  
  69.         "/* Simple ANSI C example */    ",
  70.         "                               ",
  71.         "#include <defs.h>              ",
  72.         "                               ",
  73.         "int                            ",
  74.         "main(int argc, char **argv)    ",
  75.         "{                              ",
  76.         "   // anybody at home ?        ",
  77.         "                               ",
  78.         "   puts(\"Hello world !\");    ",
  79.         "}                              ",
  80.  
  81.         NULL
  82.     };
  83.  
  84.     // color suggestions
  85.  
  86.     static ULONG levelColors[] = {
  87.  
  88.         MAKE_RGB4(0,  0,  0),
  89.         MAKE_RGB4(15, 15, 0),
  90.     };
  91.  
  92.     parserData.pd_Release  = SCANLIBVERSION;
  93.     parserData.pd_Version  = 1;
  94.     parserData.pd_Serial   = 0;
  95.     parserData.pd_Info     = "Example 2.1";
  96.     parserData.pd_Levels   = 2;
  97.     parserData.pd_Names    = levelNames;
  98.     parserData.pd_Colors   = levelColors;
  99.     parserData.pd_Flags    = 0;
  100.     parserData.pd_Example  = example;
  101.  
  102.     return(&parserData);
  103. }
  104.  
  105.  
  106. /* ------------------------------- StartScanner --------------------------------
  107.  
  108.  Called by the editor after a new text buffer has been created. We allocate a
  109.  buffer to hold text-specific data. The buffer address is returned as handle.
  110.  
  111. */
  112.  
  113. LibCall ULONG
  114. StartScanner(__A0 struct GlobalConfig *globalConfigPtr, __A1 struct EditConfig *editConfigPtr, __D0 struct SyntaxChunk *syntaxStack)
  115. {
  116.     struct BufferHandle *handle;
  117.  
  118.     if (handle = AllocVec(sizeof(struct BufferHandle), MEMF_PUBLIC | MEMF_CLEAR)) {
  119.  
  120.         handle->bh_GlobalConfig = globalConfigPtr;
  121.         handle->bh_EditConfig   = editConfigPtr;
  122.         handle->bh_SyntaxStack  = syntaxStack;
  123.     }
  124.  
  125.     return((ULONG)handle);
  126. }
  127.  
  128.  
  129. /* ------------------------------- CloseScanner --------------------------------
  130.  
  131.  Called by the editor if a text buffer is about to be closed. Deallocate buffer
  132.  specific 'global' data.
  133.  
  134. */
  135.  
  136. LibCall ULONG
  137. CloseScanner(__D0 ULONG scanID)
  138. {
  139.     if (scanID) {
  140.  
  141.         struct BufferHandle *handle = (struct BufferHandle *)scanID;
  142.  
  143.         FreeVec(handle);
  144.     }
  145.  
  146.     return(0);
  147. }
  148.  
  149.  
  150. /* ------------------------------- FlushScanner --------------------------------
  151.  
  152.  Called by the editor in low memory situations: we are supposed to free as much
  153.  memory as possible. We don't use a syntax cache, so there is nothing to free.
  154.  
  155. */
  156.  
  157. LibCall void
  158. FlushScanner(__D0 ULONG scanID)
  159. {
  160.     ;
  161. }
  162.  
  163.  
  164. /* ------------------------------- SetupScanner --------------------------------
  165.  
  166.  Called by the editor if the user wants to change the scanner's configuration.
  167.  We do not support user configuration (parserData.pd_Flags: SCPRF_CONFIGWIN flag
  168.  unset).
  169.  
  170. */
  171.  
  172. LibCall void
  173. SetupScanner(__A0 globalConfigPtr)
  174. {
  175.     ;
  176. }
  177.  
  178.  
  179. /* ------------------------------- BriefScanner --------------------------------
  180.  
  181.  Called to notify a context scanner if lines have been added, deleted or
  182.  modified. We aren't a context scanner (parserData.pd_Flags: SCPRF_CONTEXT
  183.  flag unset), so we won't ever have to request additional display requests:
  184.  the editor's built-in refresh of damage regions is sufficient.
  185.  
  186. */
  187.  
  188. LibCall struct RefreshRequest *
  189. BriefScanner(__D0 ULONG scanID, __A0 struct ScannerNotify *notify)
  190. {
  191.     return(NULL);
  192. }
  193.  
  194.  
  195. /* --------------------------------- ParseLine ---------------------------------
  196.  
  197.  Parse a line, build a syntax description
  198.  
  199. */
  200.  
  201. LibCall struct SyntaxChunk *
  202. ParseLine(__D0 ULONG scanID, __A0 struct LineNode *lineNode, __D1 ULONG line)
  203. {
  204.     if (IS_FOLD(lineNode))
  205.  
  206.         return(NULL);
  207.  
  208.     else if (lineNode->Len)
  209.  
  210.         return(ParseString(lineNode->Text, lineNode->Len, scanID));
  211.     else
  212.         return(NULL);
  213. }
  214.  
  215. /* -------------------------------- UnparseLines -------------------------------
  216.  
  217.  Called by the editor if lines are to be deleted. We are supposed to free
  218.  private data attached to the lines. This scanner doesn't attach private data to
  219.  lines (context scanners usally do in order to implement a syntax cache), so
  220.  there is nothing to be freed.
  221.  
  222. */
  223.  
  224. LibCall void
  225. UnparseLines(__A0  struct LineNode *lineNode, __D0 ULONG lines)
  226. {
  227.     ;
  228. }
  229.  
  230. /* -------------------------------- ParseSection -------------------------------
  231.  
  232.  Called by the editor if lines are to be displayed. The scanner is encouraged to
  233.  preparse the lines. This scanner doesn't attach private data to lines (context
  234.  scanners usally do in order to implement a syntax cache), so there is nothing
  235.  to do.
  236.  
  237. */
  238.  
  239. LibCall void
  240. ParseSection(__D0 ULONG scanID, __A0  struct LineNode *lineNode, __D1 ULONG lines)
  241. {
  242.     ;
  243. }
  244.  
  245. ///
  246. /// "private"
  247.  
  248. /* -------------------------------- ParseString --------------------------------
  249.  
  250.  Parse a string, build a syntax description. This is a simple example only:
  251.  C++-Comments (// ....) are highlighted. Return EMPTY_STACK in case there is
  252.  nothing to highlight.
  253.  
  254. */
  255.  
  256. struct SyntaxChunk *
  257. ParseString(UBYTE *text, UWORD len, ULONG scanID)
  258. {
  259.     if (len) {
  260.  
  261.         BOOL  inString;
  262.         UWORD indent;
  263.  
  264.         // leading spaces have to be ignored
  265.  
  266.         for (indent = 0; len && (*text == 32); ++indent, --len)
  267.             ++text;
  268.  
  269.         // trailing spaces have to be ignored
  270.  
  271.         while (len && (text[len - 1] == 32))
  272.             --len;
  273.  
  274.         // check whether line containes a C++comment
  275.  
  276.         for (inString = FALSE; len >= 2; ++text, ++indent, --len) {
  277.  
  278.             if (*text == 34)
  279.  
  280.                 inString = !inString;
  281.  
  282.             else if (inString == FALSE) {
  283.  
  284.                 if ((text[0] == '/') && (text[1] == '/')) {
  285.  
  286.                     struct SyntaxChunk *syntaxStack = ((struct BufferHandle *)scanID)->bh_SyntaxStack;
  287.  
  288.                     syntaxStack[0].sc_Level = 1;
  289.                     syntaxStack[0].sc_Start = indent;
  290.                     syntaxStack[0].sc_End   = indent + len - 1;
  291.  
  292.                     syntaxStack[1].sc_Start = FALSE;
  293.                     syntaxStack[1].sc_End   = FALSE;
  294.                     syntaxStack[1].sc_Level = FALSE;
  295.  
  296.                     return(syntaxStack);
  297.                 }
  298.             }
  299.         }
  300.     }
  301.  
  302.     return(NULL);
  303. }
  304.  
  305. ///
  306.