lex.h 3.5 KB

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