home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / js / src / jsscan.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  10.1 KB  |  238 lines

  1. /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #ifndef jsscan_h___
  20. #define jsscan_h___
  21. /*
  22.  * JS lexical scanner interface.
  23.  */
  24. #include <stddef.h>
  25. #ifdef JSFILE
  26. #include <stdio.h>
  27. #endif
  28. #include "jsopcode.h"
  29. #include "jsprvtd.h"
  30. #include "jspubtd.h"
  31.  
  32. PR_BEGIN_EXTERN_C
  33.  
  34. typedef enum JSTokenType {
  35.     TOK_ERROR = -1,                     /* well-known as the only code < EOF */
  36.     TOK_EOF,                            /* end of file */
  37.     TOK_EOL,                            /* end of line */
  38.     TOK_SEMI,                           /* semicolon */
  39.     TOK_LB, TOK_RB,                     /* left and right brackets */
  40.     TOK_LC, TOK_RC,                     /* left and right curlies (braces) */
  41.     TOK_LP, TOK_RP,                     /* left and right parentheses */
  42.     TOK_COMMA,                          /* comma operator */
  43.     TOK_ASSIGN,                         /* assignment ops (= += -= etc.) */
  44.     TOK_HOOK, TOK_COLON,                /* conditional (?:) */
  45.     TOK_OR,                             /* logical or (||) */
  46.     TOK_AND,                            /* logical and (&&) */
  47.     TOK_BITOR,                          /* bitwise-or (|) */
  48.     TOK_BITXOR,                         /* bitwise-xor (^) */
  49.     TOK_BITAND,                         /* bitwise-and (&) */
  50.     TOK_EQOP,                           /* equality ops (== !=) */
  51.     TOK_RELOP,                          /* relational ops (< <= > >=) */
  52.     TOK_SHOP,                           /* shift ops (<< >> >>>) */
  53.     TOK_PLUS,                           /* plus */
  54.     TOK_MINUS,                          /* minus */
  55.     TOK_STAR, TOK_DIVOP,                /* multiply/divide ops (* / %) */
  56.     TOK_UNARYOP,                        /* unary prefix operator */
  57.     TOK_INC, TOK_DEC,                   /* increment/decrement (++ --) */
  58.     TOK_DOT,                            /* member operator (.) */
  59.     TOK_NAME,                           /* identifier */
  60.     TOK_NUMBER,                         /* numeric constant */
  61.     TOK_STRING,                         /* string constant */
  62.     TOK_OBJECT,                         /* RegExp or other object constant */
  63.     TOK_PRIMARY,                        /* true, false, null, this, super */
  64.     TOK_FUNCTION,                       /* function keyword */
  65.     TOK_EXPORT, TOK_IMPORT,             /* export keyword */
  66.     TOK_IF,                             /* if keyword */
  67.     TOK_ELSE,                           /* else keyword */
  68.     TOK_SWITCH,                         /* switch keyword */
  69.     TOK_CASE,                           /* case keyword */
  70.     TOK_DEFAULT,                        /* default keyword */
  71.     TOK_WHILE,                          /* while keyword */
  72.     TOK_DO,                             /* do keyword */
  73.     TOK_FOR,                            /* for keyword */
  74.     TOK_BREAK,                          /* break keyword */
  75.     TOK_CONTINUE,                       /* continue keyword */
  76.     TOK_IN,                             /* in keyword */
  77.     TOK_VAR,                            /* var keyword */
  78.     TOK_WITH,                           /* with keyword */
  79.     TOK_RETURN,                         /* return keyword */
  80.     TOK_NEW,                            /* new keyword */
  81.     TOK_DELETE,                         /* delete keyword */
  82.     TOK_DEFSHARP, TOK_USESHARP,         /* #n=, #n# for object/array literals */
  83.     TOK_RESERVED,                       /* reserved keywords */
  84.     TOK_LIMIT                           /* domain size */
  85. } JSTokenType;
  86.  
  87. #define IS_PRIMARY_TOKEN(tt) (TOK_NAME <= (tt) && (tt) <= TOK_PRIMARY)
  88.  
  89. struct JSToken {
  90.     JSTokenType         type;           /* char value or above enumerator */
  91.     jschar              *ptr;           /* beginning of token in line buffer */
  92.     union {
  93.         JSAtom          *atom;          /* atom table entry */
  94.         jsdouble        dval;           /* floating point number */
  95.         JSOp            op;             /* operator, for minimal parser */
  96.     } u;
  97. };
  98.  
  99. typedef struct JSTokenBuf {
  100.     jschar              *base;          /* base of line or stream buffer */
  101.     jschar              *limit;         /* limit for quick bounds check */
  102.     jschar              *ptr;           /* next char to get, or slot to use */
  103. } JSTokenBuf;
  104.  
  105. #define JS_LINE_LIMIT   256             /* logical line buffer size limit --
  106.                                            physical line length is unlimited */
  107.  
  108. struct JSTokenStream {
  109.     JSToken             token;          /* last token scanned */
  110.     JSToken             pushback;       /* pushed-back already-scanned token */
  111.     uint16              newlines;       /* newlines skipped during lookahead */
  112.     uint16              flags;          /* flags -- see below */
  113.     uint16              lineno;         /* current line number */
  114.     uintN               ungetpos;       /* next free char slot in ungetbuf */
  115.     jschar              ungetbuf[4];    /* at most 4, for \uXXXX lookahead */
  116.     JSTokenBuf          linebuf;        /* line buffer for diagnostics */
  117.     JSTokenBuf          userbuf;        /* user input buffer if !file */
  118.     JSTokenBuf          tokenbuf;       /* current token string buffer */
  119.     const char          *filename;      /* input filename or null */
  120. #ifdef JSFILE
  121.     FILE                *file;          /* stdio stream if reading from file */
  122. #endif
  123.     JSPrincipals        *principals;    /* principals associated with given input */
  124. };
  125.  
  126. /* JSTokenStream flags */
  127. #define TSF_ERROR       0x0001          /* fatal error while scanning */
  128. #define TSF_EOF         0x0002          /* hit end of file */
  129. #define TSF_NEWLINES    0x0004          /* tokenize newlines */
  130. #define TSF_FUNCTION    0x0008          /* scanning inside function body */
  131. #define TSF_RETURN_EXPR 0x0010          /* function has 'return expr;' */
  132. #define TSF_RETURN_VOID 0x0020          /* function has 'return;' */
  133. #define TSF_INTERACTIVE 0x0040          /* interactive parsing mode */
  134. #define TSF_COMMAND     0x0080          /* command parsing mode */
  135. #define TSF_LOOKAHEAD   0x0100          /* looking ahead for a token */
  136. #define TSF_REGEXP      0x0200          /* looking for a regular expression */
  137.  
  138. /*
  139.  * At most one non-EOF token can be pushed back onto a TokenStream between
  140.  * Get or successful Match operations.  These macros manipulate the pushback
  141.  * token to clear it when changing scanning modes (upon initialzation, after
  142.  * errors, or between newline-sensitive and insensitive states).
  143.  */
  144. #define CLEAR_PUSHBACK(ts)  ((ts)->pushback.type = TOK_EOF)
  145. #define SCAN_NEWLINES(ts)   ((ts)->flags |= TSF_NEWLINES)
  146. #define HIDE_NEWLINES(ts)                                                     \
  147.     PR_BEGIN_MACRO                                                            \
  148.     (ts)->flags &= ~TSF_NEWLINES;                                         \
  149.     if ((ts)->pushback.type == TOK_EOL)                                   \
  150.         (ts)->pushback.type = TOK_EOF;                                    \
  151.     PR_END_MACRO
  152.  
  153. /*
  154.  * Return the current line number by subtracted newlines skipped during
  155.  * lookahead that was pushed back and has yet to be rescanned.
  156.  */
  157. #define TRUE_LINENO(ts) ((ts)->lineno - (ts)->newlines)
  158.  
  159. /*
  160.  * Create a new token stream, either from an input buffer or from a file.
  161.  * Return null on file-open or memory-allocation failure.
  162.  *
  163.  * NB: Both js_New{Buffer,File}TokenStream() return a pointer to transient
  164.  * memory in the current context's temp pool.  This memory is deallocated via
  165.  * PR_ARENA_RELEASE() after parsing is finished.
  166.  */
  167. extern JSTokenStream *
  168. js_NewTokenStream(JSContext *cx, const jschar *base, size_t length,
  169.           const char *filename, uintN lineno, JSPrincipals *principals);
  170.  
  171. extern JS_FRIEND_API(JSTokenStream *)
  172. js_NewBufferTokenStream(JSContext *cx, const jschar *base, size_t length);
  173.  
  174. extern JS_FRIEND_API(JSTokenStream *)
  175. js_NewFileTokenStream(JSContext *cx, const char *filename);
  176.  
  177. extern JS_FRIEND_API(JSBool)
  178. js_CloseTokenStream(JSContext *cx, JSTokenStream *ts);
  179.  
  180. /*
  181.  * Initialize the scanner, installing JS keywords into cx's global scope.
  182.  */
  183. extern JSBool
  184. js_InitScanner(JSContext *cx);
  185.  
  186. /*
  187.  * Friend-exported API entry point to call a mapping function on each reserved
  188.  * identifier in the scanner's keyword table.
  189.  */
  190. extern JS_FRIEND_API(void)
  191. js_MapKeywords(void (*mapfun)(const char *));
  192.  
  193. /*
  194.  * Report an error found while scanning ts to a window or other output device
  195.  * associated with cx.
  196.  */
  197. extern void
  198. js_ReportCompileError(JSContext *cx, JSTokenStream *ts, const char *format,
  199.               ...);
  200.  
  201. /*
  202.  * Look ahead one token and return its type.
  203.  */
  204. extern JSTokenType
  205. js_PeekToken(JSContext *cx, JSTokenStream *ts, JSCodeGenerator *cg);
  206.  
  207. extern JSTokenType
  208. js_PeekTokenSameLine(JSContext *cx, JSTokenStream *ts, JSCodeGenerator *cg);
  209.  
  210. /*
  211.  * Get the next token from ts.
  212.  */
  213. extern JSTokenType
  214. js_GetToken(JSContext *cx, JSTokenStream *ts, JSCodeGenerator *cg);
  215.  
  216. /*
  217.  * Push back the last scanned token onto ts.
  218.  */
  219. extern void
  220. js_UngetToken(JSTokenStream *ts);
  221.  
  222. /*
  223.  * Get the next token from ts if its type is tt.
  224.  */
  225. extern JSBool
  226. js_MatchToken(JSContext *cx, JSTokenStream *ts, JSCodeGenerator *cg,
  227.           JSTokenType tt);
  228.  
  229. /*
  230.  * Emit pending newline source notes counted while (mis-)matching lookahead.
  231.  */
  232. extern JSBool
  233. js_FlushNewlines(JSContext *cx, JSTokenStream *ts, JSCodeGenerator *cg);
  234.  
  235. PR_END_EXTERN_C
  236.  
  237. #endif /* jsscan_h___ */
  238.