lex.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* $Id: lex.h,v 1.24 2007/02/11 20:07:31 dijkstra Exp $ */
  2. /*
  3. * Copyright (c) 2001-2007 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_FROM 11
  52. #define LXT_IF 12
  53. #define LXT_IN 13
  54. #define LXT_IO 14
  55. #define LXT_IO1 15
  56. #define LXT_MBUF 16
  57. #define LXT_MEM 17
  58. #define LXT_MONITOR 18
  59. #define LXT_MUX 19
  60. #define LXT_OPEN 20
  61. #define LXT_PF 21
  62. #define LXT_PFQ 22
  63. #define LXT_PORT 23
  64. #define LXT_PROC 24
  65. #define LXT_SECOND 25
  66. #define LXT_SECONDS 26
  67. #define LXT_SENSOR 27
  68. #define LXT_SOURCE 28
  69. #define LXT_STREAM 29
  70. #define LXT_TO 30
  71. #define LXT_WRITE 31
  72. struct lex {
  73. char *buffer; /* current line(s) */
  74. const char *filename;
  75. int fh;
  76. char *token; /* last token seen */
  77. long value; /* value of last token seen, if num */
  78. int bsize; /* size of buffer */
  79. int cline; /* current lineno */
  80. int curpos; /* current position in buffer */
  81. int endpos; /* current maxpos in buffer */
  82. int op; /* opcode of token, if string */
  83. int unget; /* bool; token pushed back */
  84. int tokpos; /* current position in token buffer */
  85. enum {
  86. LXY_STRING, LXY_NUMBER, LXY_UNKNOWN
  87. }
  88. type; /* type of token in buffer */
  89. };
  90. __BEGIN_DECLS
  91. char *parse_opcode(int);
  92. int lex_nexttoken(struct lex *);
  93. int parse_token(const char *);
  94. struct lex *open_lex(const char *);
  95. void close_lex(struct lex *);
  96. void lex_ungettoken(struct lex *);
  97. void parse_error(struct lex *, const char *);
  98. void reset_lex(struct lex *);
  99. void rewind_lex(struct lex *);
  100. __END_DECLS
  101. /* EXPECT(l,x) = next token in l must be opcode x or error. */
  102. #define EXPECT(l, x) do { \
  103. lex_nexttoken((l)); \
  104. if ((l)->op != (x)) { \
  105. parse_error((l), parse_opcode((x))); \
  106. return 0; \
  107. } \
  108. } while (0);
  109. #endif /* _SYMON_LIB_LEX_H */