Skip to content
parser.h 2.93 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 Fri Sep  1 00:21:07 2006 Seblu
Seblu's avatar
Seblu committed
*/

Seblu's avatar
Seblu committed
#include <setjmp.h>
Seblu's avatar
Seblu committed
#include "../ast/ast.h"

#ifndef PARSER_H_
# define PARSER_H_

Seblu's avatar
Seblu committed
# include "getline.h"

// Define is parser or lexer is run for DEBBUGING
#define DEBUG_PARSER 1
#define DEBUG_LEXER 0

Seblu's avatar
Seblu committed
typedef enum		tokenid
Seblu's avatar
Seblu committed
  {
    //token free-context recognition (lexer time)
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,		// ;
    TOK_DLESSDASH,	// <<-
    TOK_DLESS,		// <<
    TOK_LESSGREAT,	// <>
    TOK_LESSAND,	// <&
    TOK_LESS,		// <
    TOK_DGREAT,		// >>
    TOK_GREATAND,	// >&
    TOK_CLOBBER,	// >|
    TOK_GREAT,		// >
    TOK_WORD,		// all others
    //token context-sensitive recognition (parser time)
Seblu's avatar
Seblu committed
    TOK_IF,
    TOK_THEN,
    TOK_ELSE,
    TOK_FI,
    TOK_ELIF,
    TOK_DO,
    TOK_DONE,
    TOK_CASE,
    TOK_ESAC,
    TOK_WHILE,
    TOK_UNTIL,
    TOK_FOR,
    TOK_IN,
    TOK_LBRACE,
    TOK_RBRACE,
    TOK_NUMBER,
    TOK_ASSIGNMENT,
    TOK_BANG
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		token;
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

/*!
** Start a new lexical recognition
** This must be call by parser before each parse start
** This function is necessarity to correctly show the prompt
Seblu's avatar
Seblu committed
**
Seblu's avatar
Seblu committed
** @param lexer lexer to set in starting block
**
** @return return if lexer is ready to start or not
Seblu's avatar
Seblu committed
*/
Seblu's avatar
Seblu committed
int			lexer_start(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

/*!
** 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