Skip to content
parser.h 3.07 KiB
Newer Older
Seblu's avatar
Seblu committed
/*
** parser.h for 42sh
**
** Made by Seblu
** Login   <seblu@epita.fr>
**
** Started on  Wed Aug  2 00:49:50 2006 Seblu
** Last update Sun Nov 12 02:55:47 2006 seblu
Seblu's avatar
Seblu committed
*/

#ifndef PARSER_H_
# define PARSER_H_

Seblu's avatar
Seblu committed
# include <setjmp.h>
# include "../ast/ast.h"
Seblu's avatar
Seblu committed
# include "getline.h"
// Define is parser or lexer is running for DEBBUGING
# define DEBUG_PARSER 0
Seblu's avatar
Seblu committed
# define DEBUG_LEXER 0

enum {
Seblu's avatar
--  
Seblu committed
  TOKEN_COUNT = 23,
Seblu's avatar
Seblu committed
  KEYWORD_COUNT = 15,
Seblu's avatar
--  
Seblu committed
  FD_MAX = 32765,
  REGISTER_REDUCE_SIZE = 200,
  REGISTER_DEFAULT_SIZE = 50,
Seblu's avatar
Seblu committed
};
Seblu's avatar
Seblu committed
typedef enum		tokenid
Seblu's avatar
Seblu committed
  {
Seblu's avatar
Seblu committed
    TOK_NONE,
    TOK_NEWLINE,	// \n
    TOK_EOF,		// EOF
    TOK_AND,		// &&
    TOK_SEPAND,		// &
    TOK_OR,		// ||
    TOK_PIPE,		// |
    TOK_DSEMI,		// ;;
    TOK_SEP,		// ;
Seblu's avatar
Seblu committed
    TOK_LPAREN,		// (
    TOK_RPAREN,		// )
    TOK_DLESSDASH,	// <<-
    TOK_DLESS,		// <<
    TOK_LESSGREAT,	// <>
    TOK_LESSAND,	// <&
    TOK_LESS,		// <
    TOK_DGREAT,		// >>
    TOK_GREATAND,	// >&
    TOK_CLOBBER,	// >|
    TOK_GREAT,		// >
    TOK_IONUMBER,	// number juste before '>' or '<'
Seblu's avatar
Seblu committed
    TOK_WORD		// all others
Seblu's avatar
Seblu committed
  } e_tokenid;
Seblu's avatar
Seblu committed

Seblu's avatar
Seblu committed
typedef struct		token
Seblu's avatar
Seblu committed
{
Seblu's avatar
Seblu committed
  e_tokenid		id;
  char			*str;
  size_t		len;
Seblu's avatar
Seblu committed
} s_token;
Seblu's avatar
Seblu committed

Seblu's avatar
Seblu committed
typedef struct		lexer
Seblu's avatar
Seblu committed
{
Seblu's avatar
--  
Seblu committed
  s_token		current; //working token
  s_token		previous;
Seblu's avatar
Seblu committed
  FILE			*fs;
  char			eof;
Seblu's avatar
Seblu committed
  char			*buf;
  size_t		buf_size; //without \0
Seblu's avatar
Seblu committed
  size_t		buf_pos;
Seblu's avatar
Seblu committed
  s_getline		*stream;
Seblu's avatar
Seblu committed
} s_lexer;
Seblu's avatar
Seblu committed

Seblu's avatar
Seblu committed
typedef struct		parser
Seblu's avatar
Seblu committed
{
  int			error;
Seblu's avatar
Seblu committed
  s_lexer		*lexer;
Seblu's avatar
Seblu committed
  jmp_buf		stack;
Seblu's avatar
Seblu committed
  s_ast_node		**regnodes;
Seblu's avatar
Seblu committed
  size_t		regsize;
  size_t		regpos;
Seblu's avatar
Seblu committed
} s_parser;
Seblu's avatar
Seblu committed

Seblu's avatar
Seblu committed
/*
** ==============
Seblu's avatar
Seblu committed
** FILE: parser.c
** ==============
Seblu's avatar
Seblu committed
*/

Seblu's avatar
Seblu committed
/*!
** Parser initialization
**
** @param fs file stream to read
**
** @return the new struct
*/
Seblu's avatar
Seblu committed
s_parser		*parser_init(int fd);
Seblu's avatar
Seblu committed

/*!
** Do a parse pass
**
** @param parser the parser where we do this parse
**
** @return ast_to_execute
*/
Seblu's avatar
Seblu committed
s_ast_node		*parse(s_parser *parser);
Seblu's avatar
Seblu committed

Seblu's avatar
Seblu committed
/*
Seblu's avatar
Seblu committed
** FILE: lexer.c
Seblu's avatar
Seblu committed
*/

Seblu's avatar
Seblu committed
/*!
Seblu's avatar
Seblu committed
** Lexer initialization
**
** @param fs file stream to read
**
** @return the new struct
*/
Seblu's avatar
Seblu committed
s_lexer			*lexer_init(int fd);
Seblu's avatar
Seblu committed

/*!
Seblu's avatar
Seblu committed
** Flush current information of position in the line,
** size of the line, and free line buffer. This is call
** by the parser after an error.
Seblu's avatar
--  
Seblu committed
** Flush don't reset eof flag !
Seblu's avatar
Seblu committed
**
Seblu's avatar
Seblu committed
** @param lexer lexer to flush
Seblu's avatar
Seblu committed
*/
Seblu's avatar
Seblu committed
void			lexer_flush(s_lexer *lexer);
Seblu's avatar
Seblu committed

/*!
** Return the next token and destroy it
** @warning The token MUST be freed !
Seblu's avatar
Seblu committed
**
Seblu's avatar
Seblu committed
** @param lexer lexer structure
**
** @return the next token
Seblu's avatar
Seblu committed
*/
Seblu's avatar
Seblu committed
s_token			lexer_gettoken(s_lexer *lexer);
Seblu's avatar
Seblu committed

/*!
** Return the next token without destruction of it.
** @warning The token string MUST NOT be freed !
Seblu's avatar
Seblu committed
**
Seblu's avatar
Seblu committed
** @param lexer lexer structure
Seblu's avatar
Seblu committed
**
** @return the look ahead token
Seblu's avatar
Seblu committed
*/
Seblu's avatar
Seblu committed
s_token			lexer_lookahead(s_lexer *lexer);
Seblu's avatar
Seblu committed

Seblu's avatar
--  
Seblu committed
/*!
** Return the next next token without destruction of it.
** @warning The token string MUST NOT be freed !
** This is a possibility will come from LL1 lexer.
**
** @param lexer lexer structure
**
** @return the look ahead token
*/
s_token			lexer_lookahead2(s_lexer *lexer);

/*!
** Parse input as a here-document (describe is XSI)
**
** @param lexer current lexer
*/
Seblu's avatar
Seblu committed
s_token			lexer_getheredoc(s_lexer *lexer, const char *delim);
Seblu's avatar
Seblu committed
#endif