lua源码学习-llex.h

本文深入探讨了Lua语言的词法分析器实现细节,包括关键数据结构如LexState和Token,以及各种预定义的保留字和符号。通过分析源码,揭示了Lua如何处理输入流、生成和识别token的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
** $Id: llex.h,v 1.78 2014/10/29 15:38:24 roberto Exp $
** Lexical Analyzer
** See Copyright Notice in lua.h
*/

#ifndef llex_h
#define llex_h

#include "lobject.h"
#include "lzio.h"


#define FIRST_RESERVED    257


#if !defined(LUA_ENV)
#define LUA_ENV        "_ENV"
#endif


/*
* WARNING: if you change the order of this enumeration,
* grep "ORDER RESERVED"
*
* FIRST_RESERVED==256+1
* TK_AND是TOKEN_AND的简写
*/
enum RESERVED {
  /* terminal symbols denoted(表示) by reserved words */
  TK_AND = FIRST_RESERVED, TK_BREAK,
  TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION,
  TK_GOTO, TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT,
  TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE,
  
  /* other terminal symbols */
  TK_IDIV,            /* //(整数除法) */
  TK_CONCAT,        /* .. */
  TK_DOTS,            /* ... */
  TK_EQ, TK_GE, TK_LE, TK_NE,
  TK_SHL, TK_SHR,
  TK_DBCOLON,        /* :: 形成goto标签?  ::gotoLabel:: */
  TK_EOS,                /* end of file还是看成暂未读取下一个token? */
  TK_FLT, TK_INT, TK_NAME, TK_STRING
};

/* 
 * number of reserved words
 * 其它的应该是保留的符号
*/
#define NUM_RESERVED    (cast(int, TK_WHILE-FIRST_RESERVED+1))

//语义方面的信息,分别是float,int,string三种字面常量
typedef union {
  lua_Number r;
  lua_Integer i;
  TString *ts;
} SemInfo;  /*  information */

/* 主要的结构体 */
typedef struct Token 
{
  /* 注释可能有误,以实际情况为准
  ** [FIRST_RESERVED,+N]的是关键字或操作符对应的token,eg:local, >=
  ** [1,FIRST_RESERVED),单个字符对应的token,eg: >
   */
  int token;    
  
  SemInfo seminfo;
} Token;


/* 
** state of the lexer plus state of the parser(解析器) when shared by all functions 
** 数据流
** step.1 从ZIO中读一个char
**    ZIO->n--, int tmp = *ZIO->p++
** step.2 将上一步读到的char 赋值给 LexState.current
**     LexState.current = tmp
** step.3 判断读入的tmp是否符合要求
**      符合要求:
**                    LexState.buff[idx] = LexState.current
**      不符合要求: 丢弃LexState.current,
*/
typedef struct LexState {
  /* 当前正被分析的char */
  int current;        /* current character (charint) */
  /* 上述current对应的行号 */
  int linenumber;      /* input line counter */

  /* 上一个有效token应的行号 */
  int lastline;        /* line of last token 'consumed' */
  

  struct FuncState *fs;  /* current function (parser) */
  struct lua_State *L;
  
  ZIO *z;                  /* input stream */
  Mbuffer *buff;      /* buffer for tokens(用于当前正在形成的token的buff) */

  /* 当前被分析的token(语法分析器中被使用,词法分析器中仅写入,不分析?)*/
  Token t;          /* current token */
  Token lookahead;  /* look ahead token:前看符号 */

  Table *h;         /* to avoid collection/reuse strings */
  struct Dyndata *dyd;/* dynamic structures used by the parser */
  TString *source;      /* current source name */
  TString *envn;          /* environment variable name */
  
  char decpoint;          /* locale decimal point 表示个位和小数位分割的".",不用的国家,符号不一样 */
} LexState;


LUAI_FUNC void luaX_init (lua_State *L);
LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z,
                              TString *source, int firstchar);
LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l);
LUAI_FUNC void luaX_next (LexState *ls);
LUAI_FUNC int luaX_lookahead (LexState *ls);
LUAI_FUNC l_noret luaX_syntaxerror (LexState *ls, const char *s);
LUAI_FUNC const char *luaX_token2str (LexState *ls, int token);


#endif
 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值