lex.h 3.7 KB

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