jslex.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * This file is part of Espruino, a JavaScript interpreter for Microcontrollers
  3. *
  4. * Copyright (C) 2013 Gordon Williams <gw@pur3.co.uk>
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. *
  10. * ----------------------------------------------------------------------------
  11. * Lexer (convert JsVar strings into a series of tokens)
  12. * ----------------------------------------------------------------------------
  13. */
  14. #ifndef JSLEX_H_
  15. #define JSLEX_H_
  16. #include "jsutils.h"
  17. #include "jsvar.h"
  18. typedef struct JsLex
  19. {
  20. // Actual Lexing related stuff
  21. char currCh, nextCh;
  22. short tk; ///< The type of the token that we have
  23. JslCharPos tokenStart; ///< Position in the data at the beginning of the token we have here
  24. JslCharPos tokenEnd; ///< Position in the data at the last character of the token we have here
  25. JslCharPos tokenLastStart; ///< Position in the data of the first character of the last token
  26. JslCharPos tokenLastEnd; ///< Position in the data of the last character of the last token
  27. char token[JSLEX_MAX_TOKEN_LENGTH]; ///< Data contained in the token we have here
  28. JsVar *tokenValue; ///< JsVar containing the current token - used only for strings
  29. unsigned char tokenl; ///< the current length of token
  30. /* Where we get our data from...
  31. *
  32. * This is a bit more tricky than normal because the data comes from JsVars,
  33. * which only have fixed length strings. If we go past this, we have to go
  34. * to the next jsVar...
  35. */
  36. JsVar *sourceVar; // the actual string var
  37. JsvStringIterator it; // Iterator for the string
  38. } JsLex;
  39. void jslInit(JsLex *lex, JsVar *var);
  40. void jslKill(JsLex *lex);
  41. void jslReset(JsLex *lex);
  42. void jslSeekTo(JsLex *lex, JslCharPos seekToChar); // like jslSeek, but pre-fills characters
  43. bool jslMatch(JsLex *lex, int expected_tk); ///< Match, and return true on success, false on failure
  44. void jslTokenAsString(int token, char *str, size_t len); ///< output the given token as a string - for debugging
  45. void jslGetTokenString(JsLex *lex, char *str, size_t len);
  46. char *jslGetTokenValueAsString(JsLex *lex);
  47. JsVar *jslGetTokenValueAsVar(JsLex *lex);
  48. // Only for more 'internal' use
  49. void jslSeek(JsLex *lex, JslCharPos seekToChar); // like jslSeekTo, but doesn't pre-fill characters
  50. void jslGetNextCh(JsLex *lex);
  51. void jslGetNextToken(JsLex *lex); ///< Get the text token from our text string
  52. #endif /* JSLEX_H_ */