Skip to content
parser.h 2.82 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
Seblu's avatar
Seblu committed
** Last update Wed Aug 23 02:33:38 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
typedef enum		e_token
Seblu's avatar
Seblu committed
  {
Seblu's avatar
Seblu committed
    //token free-context recognition
    TOK_NONE,
    TOK_NEWLINE,
    TOK_NUMBER,
    TOK_EOF,
Seblu's avatar
Seblu committed
    TOK_AND,
    TOK_OR,
    TOK_DSEMI,
Seblu's avatar
Seblu committed
    TOK_LESS,
    TOK_GREAT,
Seblu's avatar
Seblu committed
    TOK_DLESS,
    TOK_DGREAT,
    TOK_LESSAND,
    TOK_GREATAND,
    TOK_LESSGREAT,
    TOK_DLESSDASH,
    TOK_CLOBBER,
Seblu's avatar
Seblu committed
    TOK_CONTEXT,
    TOK_WORD,
    //token context-sensitive recognition
    TOK_ASSIGNMENT,
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_BANG,
    TOK_SEP,
    TOK_SEPAND,
  } te_tokenid;
Seblu's avatar
Seblu committed

Seblu's avatar
Seblu committed
typedef struct		s_token
Seblu's avatar
Seblu committed
{
Seblu's avatar
Seblu committed
  te_tokenid id;
Seblu's avatar
Seblu committed
  const char *str;
} ts_token;

Seblu's avatar
Seblu committed
typedef enum		e_lexer_status
Seblu's avatar
Seblu committed
  {
Seblu's avatar
Seblu committed
    LEXER_READY = 1,
    LEXER_END = 2,
  } te_lexer_status;
Seblu's avatar
Seblu committed

Seblu's avatar
Seblu committed
typedef struct		s_lexer
Seblu's avatar
Seblu committed
{
Seblu's avatar
Seblu committed
  te_lexer_status	status;
Seblu's avatar
Seblu committed
  FILE			*fs;
  char			*buf;
  size_t		buf_size;
  size_t		buf_pos;
Seblu's avatar
Seblu committed
} ts_lexer;

typedef struct		s_parser
{
  int			error;
  ts_lexer		*lexer;
  jmp_buf		stack;
  ts_ast_node		**regnodes;
  size_t		regsize;
  size_t		regpos;
Seblu's avatar
Seblu committed
} ts_parser;

Seblu's avatar
Seblu committed

/*!
** Free the string of a token if is a word (dynamically allocated)
**
** @param token a token struct type (not a pointer)
*/
#define token_free(token) if ((token).id == TOK_WORD && (token).str) \
 free((char*)(token).str)

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
*/
ts_parser		*parser_init(FILE *fs);

/*!
** Do a parse pass
**
** @param parser the parser where we do this parse
**
** @return ast_to_execute
*/
ts_ast_node		*parse(ts_parser *parser);

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
*/
ts_lexer		*lexer_init(FILE *fs);

/*!
** 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(ts_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
ts_token		lexer_gettoken(ts_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
ts_token		lexer_lookahead(ts_lexer *lexer);
Seblu's avatar
Seblu committed

#endif