lex.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2001-2010 Willem Dijkstra
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * - Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * - Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following
  13. * disclaimer in the documentation and/or other materials provided
  14. * with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  19. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  20. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  22. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  26. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. */
  30. /*
  31. * This file defines the keyword tokens and lexer structure for the simple
  32. * lexical analyser.
  33. */
  34. #ifndef _SYMON_LIB_LEX_H
  35. #define _SYMON_LIB_LEX_H
  36. #include <sys/cdefs.h>
  37. #include <stdio.h>
  38. /* Tokens known to lex */
  39. #define LXT_ACCEPT 1
  40. #define LXT_BADTOKEN 0
  41. #define LXT_BEGIN 2
  42. #define LXT_CLOSE 3
  43. #define LXT_COMMA 4
  44. #define LXT_CPU 5
  45. #define LXT_CPUIOW 6
  46. #define LXT_DATADIR 7
  47. #define LXT_DEBUG 8
  48. #define LXT_DF 9
  49. #define LXT_END 10
  50. #define LXT_EVERY 11
  51. #define LXT_FLUKSO 12
  52. #define LXT_FROM 13
  53. #define LXT_IF 14
  54. #define LXT_IF1 15
  55. #define LXT_IN 16
  56. #define LXT_IO 17
  57. #define LXT_IO1 18
  58. #define LXT_LOAD 19
  59. #define LXT_MBUF 20
  60. #define LXT_MEM 21
  61. #define LXT_MEM1 22
  62. #define LXT_MONITOR 23
  63. #define LXT_MUX 24
  64. #define LXT_OPEN 25
  65. #define LXT_PF 26
  66. #define LXT_PFQ 27
  67. #define LXT_PORT 28
  68. #define LXT_PROC 29
  69. #define LXT_SECOND 30
  70. #define LXT_SECONDS 31
  71. #define LXT_SENSOR 32
  72. #define LXT_SMART 33
  73. #define LXT_SOURCE 34
  74. #define LXT_STREAM 35
  75. #define LXT_TO 36
  76. #define LXT_WRITE 37
  77. struct lex {
  78. char *buffer; /* current line(s) */
  79. const char *filename;
  80. int fh;
  81. char *token; /* last token seen */
  82. long value; /* value of last token seen, if num */
  83. int bsize; /* size of buffer */
  84. int cline; /* current lineno */
  85. int curpos; /* current position in buffer */
  86. int endpos; /* current maxpos in buffer */
  87. int op; /* opcode of token, if string */
  88. int unget; /* bool; token pushed back */
  89. int eof; /* bool; all tokens read */
  90. int tokpos; /* current position in token buffer */
  91. enum {
  92. LXY_STRING, LXY_NUMBER, LXY_UNKNOWN
  93. }
  94. type; /* type of token in buffer */
  95. };
  96. __BEGIN_DECLS
  97. char *parse_opcode(int);
  98. int lex_nexttoken(struct lex *);
  99. int parse_token(const char *);
  100. struct lex *open_lex(const char *);
  101. void close_lex(struct lex *);
  102. void lex_ungettoken(struct lex *);
  103. void parse_error(struct lex *, const char *);
  104. void reset_lex(struct lex *);
  105. void rewind_lex(struct lex *);
  106. __END_DECLS
  107. /* EXPECT(l,x) = next token in l must be opcode x or error. */
  108. #define EXPECT(l, x) do { \
  109. lex_nexttoken((l)); \
  110. if ((l)->op != (x)) { \
  111. parse_error((l), parse_opcode((x))); \
  112. return 0; \
  113. } \
  114. } while (0);
  115. #endif /* _SYMON_LIB_LEX_H */