libcm is a C development framework with an emphasis on audio signal processing applications.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cmLex.h 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //| Copyright: (C) 2009-2020 Kevin Larke <contact AT larke DOT org>
  2. //| License: GNU GPL version 3.0 or above. See the accompanying LICENSE file.
  3. #ifndef cmLex_h
  4. #define cmLex_h
  5. //( { file_desc:"User configurable lexer for tokenizing text files." kw:[text]}
  6. // Predefined Lexer Id's
  7. enum
  8. {
  9. kErrorLexTId, // 0 the lexer was unable to identify the current token
  10. kUnknownLexTId, // 1 the token is of an unknown type (only used when kReturnUnknownLexFl is set)
  11. kEofLexTId, // 2 the lexer reached the end of input
  12. kSpaceLexTId, // 3 white space
  13. kRealLexTId, // 4 real number (contains a decimal point or is in scientific notation)
  14. kIntLexTId, // 5 decimal integer
  15. kHexLexTId, // 6 hexidecimal integer
  16. kIdentLexTId, // 7 identifier
  17. kQStrLexTId, // 8 quoted string
  18. kQCharLexTId, // 9 quoted char
  19. kBlockCmtLexTId, // 10 block comment
  20. kLineCmtLexTId, // 11 line comment
  21. kUserLexTId // 12 user registered token (See cmLexRegisterToken().)
  22. };
  23. // Lexer control flags used with cmLexInit().
  24. enum
  25. {
  26. kReturnSpaceLexFl = 0x01, //< Return space tokens
  27. kReturnCommentsLexFl = 0x02, //< Return comment tokens
  28. kReturnUnknownLexFl = 0x04, //< Return unknown tokens
  29. kReturnQCharLexFl = 0x08, //< Return quoted characters
  30. kUserDefPriorityLexFl= 0x10 //< User defined tokens take priority even if a kIdentLexTId token has a longer match
  31. };
  32. // cmLex result codes.
  33. enum
  34. {
  35. kOkLexRC = cmOkRC, //< 0 No error. The operation completed successfully
  36. kDuplicateTokenLexRC, //< 1 The text or id passed as a user token is already in use by another token
  37. kMissingCmtEndLexRC, //< 2 The end of a block comment could not be found.
  38. kMissingEndQuoteLexRC, //< 3 The end of a quoted string could not be found.
  39. kNoMatchLexRC, //< 4 The lexer encountered a string which could not be classified.
  40. kFileOpenErrLexRC, //< 5 File open failed on cmLexSetFile()
  41. kFileSeekErrLexRC, //< 6 File seek failed on cmLexSetFile()
  42. kFileTellErrLexRC, //< 7 File tell failed on cmLexSetFile()
  43. kFileReadErrLexRC, //< 8 File read failed on cmLexSetFile()
  44. kFileCloseErrLexRC, //< 9 File close failed on cmLexSetFile()
  45. kMemAllocErrLexRC, //< 10 An attempted memory allocation failed
  46. kEofRC, //< 11 The end of the input text was encountered (this is a normal condition not an error)
  47. kInvalidLexTIdLexRC, //< 12 An invalid lex token id was encountered.
  48. kSignErrorLexRC, //< 13 An signed integer has a 'u' or 'U' suffix."
  49. kInvalidLexRC //< 1r Sentinal value.
  50. };
  51. typedef cmHandle_t cmLexH;
  52. extern cmLexH cmLexNullH;
  53. // Iniitalize the lexer and receive a lexer handle in return.
  54. // Set cp to NULL if the buffer will be later via cmLexSetTextBuffer();
  55. // See the kXXXLexFl enum's above for possible flag values.
  56. cmLexH cmLexInit( const cmChar_t* cp, unsigned cn, unsigned flags, cmRpt_t* rpt );
  57. // Finalize a lexer created by an earlier call to cmLexInit()
  58. cmRC_t cmLexFinal( cmLexH* hp );
  59. // Rewind the lexer to the begining of the buffer (the same as post initialize state)
  60. cmRC_t cmLexReset( cmLexH h );
  61. // Verify that a lexer handle is valid
  62. bool cmLexIsValid( cmLexH h );
  63. // Set a new text buffer and reset the lexer to the post initialize state.
  64. cmRC_t cmLexSetTextBuffer( cmLexH h, const cmChar_t* cp, unsigned cn );
  65. cmRC_t cmLexSetFile( cmLexH h, const cmChar_t* fn );
  66. // Register a user defined token. The id of the first user defined token should be
  67. // kUserLexTId+1. Neither the id or token text can be used by a previously registered
  68. // or built-in token.
  69. cmRC_t cmLexRegisterToken( cmLexH h, unsigned id, const cmChar_t* token );
  70. // Register a user defined token recognition function. This function should return the count
  71. // of initial, consecutive, characters in 'cp[cn]' which match its token pattern.
  72. typedef unsigned (*cmLexUserMatcherPtr_t)( const cmChar_t* cp, unsigned cn );
  73. cmRC_t cmLexRegisterMatcher( cmLexH h, unsigned id, cmLexUserMatcherPtr_t funcPtr );
  74. // Enable or disable the specified token type.
  75. cmRC_t cmLexEnableToken( cmLexH h, unsigned id, bool enableFl );
  76. // Get and set the lexer filter flags kReturnXXXLexFl.
  77. // These flags can be safely enabled and disabled between
  78. // calls to cmLexGetNextToken().
  79. unsigned cmLexFilterFlags( cmLexH h );
  80. void cmLexSetFilterFlags( cmLexH h, unsigned flags );
  81. // Return the type id of the current token and advances to the next token
  82. unsigned cmLexGetNextToken( cmLexH h );
  83. // Return the type id associated with the current token. This is the same value
  84. // returned by the previous call to cmLexGetNextToken().
  85. unsigned cmLexTokenId( cmLexH h );
  86. // Return a pointer to the first character of text associated with the
  87. // current token. The returned pointer directly references the text contained
  88. // in the buffer given to the lexer in the call to cmLexInit(). The string
  89. // is therefore not zero terminated. Use cmLexTokenCharCount() to get the
  90. // length of the token string.
  91. const cmChar_t* cmLexTokenText( cmLexH h );
  92. // Return the count of characters in the text associated with the current token.
  93. // This is the only way to get this count since the string returned by
  94. // cmLexTokenText() is not zero terminated.
  95. unsigned cmLexTokenCharCount( cmLexH h );
  96. // Return the value of the current token as an integer.
  97. int cmLexTokenInt( cmLexH h );
  98. // Return the value of the current token as an unsigned integer.
  99. unsigned cmLexTokenUInt( cmLexH h );
  100. // Return the value of the current token as a float.
  101. float cmLexTokenFloat( cmLexH h );
  102. // Return the value of the current token as a double.
  103. double cmLexTokenDouble( cmLexH h );
  104. // Return true if the current token is an int and it was suffixed
  105. // with 'u' to indicate that it is unsigned.
  106. bool cmLexTokenIsUnsigned( cmLexH h );
  107. // Return true if the current token is a real and it was suffexed
  108. // with 'f' to indicate that it is a single precision float.
  109. bool cmLexTokenIsSinglePrecision( cmLexH h );
  110. // Return the line number associated with the current token
  111. unsigned cmLexCurrentLineNumber( cmLexH h );
  112. // Return the starting column of the current token
  113. unsigned cmLexCurrentColumnNumber( cmLexH h );
  114. // Return the RC code associated with the last error
  115. unsigned cmLexErrorRC( cmLexH h );
  116. // Return the label associated with a token id
  117. const cmChar_t* cmLexIdToLabel( cmLexH h, unsigned typeId );
  118. // Return the text message associated with a return code.
  119. const cmChar_t* cmLexRcToMsg( unsigned rc );
  120. // Lexer testing stub.
  121. void cmLexTest( cmRpt_t* rpt );
  122. //)
  123. #endif