Skip to content
parser.h 2.32 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 Sat Aug 19 01:35:31 2006 Seblu
Seblu's avatar
Seblu committed
*/

#include <stdio.h>
#include "../ast/ast.h"

#ifndef PARSER_H_
# define PARSER_H_

Seblu's avatar
Seblu committed
typedef enum e_token
  {
    TOK_AND,
    TOK_OR,
    TOK_DSEMI,
    TOK_DLESS,
    TOK_DGREAT,
    TOK_LESSAND,
    TOK_GREATAND,
    TOK_LESSGREAT,
    TOK_DLESSDASH,
    TOK_CLOBBER,
    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_NEWLINE,
    TOK_SEP,
    TOK_SEPAND,
    TOK_WORD,
    TOK_EOF,
    TOK_BEGIN,
    TOK_NONE,
    TOK_ERR
Seblu's avatar
Seblu committed
  } te_tokenid;
Seblu's avatar
Seblu committed

typedef struct s_token
{
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_parser_status
Seblu's avatar
Seblu committed
  {
    PARSE_OK,
    PARSE_ERROR,
    PARSE_END
Seblu's avatar
Seblu committed
  } te_parser_status;
Seblu's avatar
Seblu committed

typedef struct		s_parser
{
Seblu's avatar
Seblu committed
  te_parser_status	status;
Seblu's avatar
Seblu committed
  FILE			*fs;
  char			*buf;
  size_t		buf_size;
  size_t		buf_pos;
Seblu's avatar
Seblu committed
  size_t		tok_start;
Seblu's avatar
Seblu committed
} ts_parser;

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);

/*!
** Notify a parse error
**
** @param parser parser where error appear
** @param t token near of the error
*/
void			parse_error(ts_parser *parser, ts_token t);

/*!
** 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
/*!
** Set (or reset) the lexer
** This must be call by parser before each parse start
** This function is necessarity to correctly show the prompt
Seblu's avatar
Seblu committed
**
** @param parser lexer to reset
*/
void			lexer_reset(ts_parser *parser);

/*!
** Return the next token and destroy it
** @warning The token MUST be freed !
Seblu's avatar
Seblu committed
**
** @param parser parser/lexer structure
**
** @return the next token
Seblu's avatar
Seblu committed
*/
ts_token		lexer_gettoken(ts_parser *parser);
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
**
** @param parser parser/lexer structure
Seblu's avatar
Seblu committed
**
** @return the look ahead token
Seblu's avatar
Seblu committed
*/
ts_token		lexer_lookahead(ts_parser *parser);
Seblu's avatar
Seblu committed

#endif