void EXPRESSION()
{
    if(TOKEN == plussym || TOKEN == minussym)
    {
        if(TOKEN == minussym)
        {
            GETTOKEN();
            //OPR 0 1
            printToFile(2,0,1);
            lines++;
        }
        else
        {
            GETTOKEN();
        }
    }
    TERM();
    while(TOKEN == plussym || TOKEN == minussym)
    {
        if(TOKEN == plussym)
        {
            GETTOKEN();
            TERM();
            printToFile(2,0,2);
            lines++;
        }
        else if(TOKEN == minussym)
        {
            GETTOKEN();
            TERM();
            printToFile(2,0,3);
            lines++;
        }
    }
}
Ejemplo n.º 2
0
void		ft_go_up(t_group *grp)
{
	int i;

	i = -1;
	while (++i < TERM(window->width) && TERM(curs_pos) > 0)
		ft_left_arrow(grp);
}
Ejemplo n.º 3
0
void		ft_go_down(t_group *grp)
{
	int i;

	i = -1;
	while (++i < TERM(window->width) && TERM(curs_pos) < TERM(cmd_size))
		ft_right_arrow(grp);
}
Ejemplo n.º 4
0
void	find_search(t_group *grp)
{
	t_hist *curr;

	if (TERM(curs_pos) == 0)
	{
		display_search(grp, TERM(cmd_save));
		REMOVE(&TERM(search));
		TERM(search) = SDUP(TERM(cmd_save));
		return ;
	}
	curr = grp->curr_hist == NULL ? grp->hist : grp->curr_hist;
	while (curr != NULL)
	{
		if (ft_strstr(curr->name, TERM(cmd_line)) != NULL)
		{
			display_search(grp, curr->name);
			REMOVE(&TERM(search));
			TERM(search) = SDUP(curr->name);
			break ;
		}
		curr = curr->next;
	}
	curr == NULL ? REMOVE(&TERM(search)) : 0;
}
Ejemplo n.º 5
0
void	canceled_search(t_group *grp)
{
	grp->is_search = false;
	up_clear_find(grp, TERM(cmd_save));
	ft_putstr_fd(TERM(cmd_save), 2);
	TERM(cmd_line) = SDUP(TERM(cmd_save));
	TERM(curs_pos) = LEN(TERM(cmd_line));
	TERM(cmd_size) = TERM(curs_pos);
	REMOVE(&TERM(search));
	REMOVE(&TERM(cmd_save));
}
Ejemplo n.º 6
0
void	up_clear_find(t_group *grp, char *name)
{
	int	i;

	i = -1;
	name = NULL;
	tputs(tgetstr("cr", NULL), 0, ft_getchar);
	while (++i < TERM(line))
		tputs(tgetstr("up", NULL), 0, ft_getchar);
	tputs(tgetstr("cd", NULL), 0, ft_getchar);
	show_prompt(grp, "fsh-> ", 6, "\033[1;32m");
	TERM(line) = 0;
}
Ejemplo n.º 7
0
void	display_search(t_group *grp, char *name)
{
	size_t	i;

	i = -1;
	up_clear_find(grp, name);
	ft_putendl_fd(name, 2);
	while (++i < LEN(name))
		if ((START_POS + i) % TERM(window->width) == 0)
			TERM(line) += 1;
	TERM(line) += 1;
	show_prompt(grp, "fsh: search: ", 13, "\033[1;34m");
	ft_putstr_fd(TERM(cmd_line), 2);
}
Ejemplo n.º 8
0
void term_write( unsigned char x, unsigned char y, char *s, unsigned char f ) {
  unsigned char i, j, t = 0;
  int len = strlen( s );
  for( i = 0; i < len; i++ ) {
    for( j = 0; j < sizeof( font_chars ) - 1; j++ ) {
      if( font_chars[ j ] == s[ i ] ) {
        if( TERM( x,  y ).nchar != j || TERM( x,  y ).nfont != f ) {
          TERM( x,  y ).nchar = j;
          TERM( x,  y ).nfont = f;
          if( t == 0 && write_index == 0 ) {
            TERM( x,  y ).cchar = j;
            TERM( x,  y ).cfont = f;
            TERM( x,  y ).tick = 0;
          } else {
            TERM( x,  y ).tick = -( t + write_index * 5 );
          }
          t++;
        }
        x++;
        break;
      }
    }
  }
  write_index++;
}
Ejemplo n.º 9
0
char& string::operator [](int n)
{
	acl_assert(n >= 0);
#if 1
	return (*this)[(size_t) n];
#else
	if (n >= CAP(vbf_))
	{
		int  len = CAP(vbf_);
		printf("%d: cap1: %d\n", __LINE__, CAP(vbf_));
		space(n + 1);
		printf("%d: cap2: %d\n", __LINE__, CAP(vbf_));
		int  new_len = CAP(vbf_);

		// 初始化新分配的内存
		if (new_len > len)
			memset(END(vbf_), 0, new_len - len);

		// 将 vbf_->vbuf.ptr 指向 n 字节处,同时修改 vbf_->vbuf.cnt 值
		ACL_VSTRING_AT_OFFSET(vbf_, n);
	}
	else if (n >= (int) LEN(vbf_))
	{
		ACL_VSTRING_AT_OFFSET(vbf_, n);
		ADDCH(vbf_, '\0');
		TERM(vbf_);
	}

	return (char&) (vbf_->vbuf.data[n]);
#endif
}
Ejemplo n.º 10
0
ParseTree* Parser::EXPR()
{
	ParseTree* retTree = new ParseTree();
	(*retTree).SetLeaf1(TERM()->getLeaf());
    
	if((*parser).get().token == PLUS)
    {
		(*retTree).setAlt(2);

		if(!(*parser).Advance()) ThrowParserError();
		(*retTree).SetLeaf2(EXPR()->getLeaf());
	}
    else if((*parser).get().token == MINUS)
    {
		(*retTree).setAlt(3);
		if(!(*parser).Advance()) ThrowParserError();
		(*retTree).SetLeaf2(EXPR()->getLeaf());

	}
    else
    {
		(*retTree).setAlt(1);
	}
	return retTree;
}
Ejemplo n.º 11
0
char& string::operator [](size_t n)
{
	// 当偏移位置大于所分配空间的最大位置,需要重新分配内存
	if (n >= (size_t) CAP(vbf_))
	{
		int  len = CAP(vbf_);
		space(n + 1);
		int  new_len = CAP(vbf_);

		// 初始化新分配的内存
		if (new_len > len)
			memset(END(vbf_), 0, new_len - len);

		// 将 vbf_->vbuf.ptr 指向 n 字节处,同时修改 vbf_->vbuf.cnt 值
		ACL_VSTRING_AT_OFFSET(vbf_, (int) n);
	}
	// 当偏移位置大于当前数据长度时,通过重置指针位置以修改数据长度,
	// 这样当调用 length() 方法时可以获得长度
	else if (n >= LEN(vbf_))
	{
		ACL_VSTRING_AT_OFFSET(vbf_, (int) n);
		// 因为本函数返回了偏移位置 n 处的地址引用后,调用者对此引用
		// 地址赋值,但并不会使缓冲区数据长度增加,所以此处在函数返回
		// 前调用 ADDCH 相当于调用者外部赋值后将数据长度加 1
		ADDCH(vbf_, '\0');
		// 保证最后一个字节为 \0
		TERM(vbf_);
	}

	return (char&) (vbf_->vbuf.data[n]);
}
Ejemplo n.º 12
0
void POSITIVESIMPEXPR(void){
	/*positivesimpexpr = term positivesimpexpr_tail*/
	switch(l){

	case CONST_INT: case CONST_FLOAT: case CONST_BOOLEAN: case ID: case '(':		TERM(); POSITIVESIMPEXPR_TAIL();  return;
	default:																		StdError(__func__);
	}
}
Ejemplo n.º 13
0
static const char *xml_parse_cdata(ACL_XML2 *xml, const char *data)
{
	ACL_XML2_NODE *curr_node = xml->curr_node;
	int   ch;

	while ((ch = *data) != 0) {
		if (NO_SPACE(xml))
			return data;

		data++;

		if (ch == '>') {
			if (curr_node->meta[0] == ']'
				&& curr_node->meta[1] == ']')
			{
				curr_node->status = ACL_XML2_S_MEND;
				curr_node->text_size =
					END(xml) - curr_node->text;
				ADD(xml, '\0');
				return data;
			}
			if (curr_node->meta[0] == ']') {
				ADD(xml, curr_node->meta[0]);
				curr_node->meta[0] = 0;
			}
			if (curr_node->meta[1] == ']') {
				ADD(xml, curr_node->meta[1]);
				curr_node->meta[1] = 0;
			}
		} else if (ch == ']') {
			if (curr_node->meta[0] == ']') {
				if (curr_node->meta[1] == ']') {
					ADD(xml, ']');
				} else
					curr_node->meta[1] = ']';
			} else if (curr_node->meta[1] == ']') {
				curr_node->meta[0] = ']';
				curr_node->meta[1] = 0;
				ADD(xml, ']');
			} else
				curr_node->meta[0] = ']';
		} else if (curr_node->meta[0] == ']') {
			ADD(xml, ']');
			curr_node->meta[0] = 0;

			if (curr_node->meta[1] == ']') {
				ADD(xml, ']');
				curr_node->meta[1] = 0;
			}
		} else {
			ADD(xml, ch);
		}
	}

	TERM(xml);

	return data;
}
Ejemplo n.º 14
0
/*******************************************************************************
 ***  FUNCTION SIMPLEEXPR()
 *******************************************************************************
 ***  DESCRIPTION  :  Processes SIMPLEEXPR grammar rule.
 ***
 ***  SIMPLEEXPR -> TERM MORETERM
 ***
 ******************************************************************************/
void RecursiveParser::SIMPLEEXPR(TableEntry & eplace)
{
   TableEntry tplace;

   TERM(tplace);
   MORETERM(tplace);

   eplace = tplace;
}
Ejemplo n.º 15
0
bool string::scan_line(string& out, bool nonl /* = true */,
	size_t* n /* = NULL */, bool move /* = false */)
{
	if (n)
		*n = 0;

	char* pEnd = buf_end();
	if (pEnd == NULL)
		return false;
	
	size_t len = pEnd - scan_ptr_;
	char *ln = (char*) memchr(scan_ptr_, '\n', len);
	if (ln == NULL)
		return false;

	char *next = ln + 1;
	len = ln - scan_ptr_ + 1;

	if (nonl)
	{
		ln--;
		len--;
		if (ln >= scan_ptr_ && *ln == '\r')
		{
			ln--;
			len--;
		}
		if (len > 0)
			out.append(scan_ptr_, len);
	}
	else
		out.append(scan_ptr_, len);

	if (move)
	{
		if (pEnd > next)
		{
			acl_vstring_memmove(vbf_, next, pEnd - next);
			TERM(vbf_);
			scan_ptr_ = STR(vbf_);
		}
		else
			clear();
	}
	else
	{
		if (next >= pEnd)
			clear();
		else
			scan_ptr_ = next;
	}

	if (n)
		*n = len;

	return true;
}
Ejemplo n.º 16
0
static void add(Pu *L, __pu_value *&temp)
{
	PuType tp = TOKEN.type;
	OperatorType nv = TOKEN.optype;

	while (tp == OP) // +-
	{
		switch (nv)
		{
		case OPT_ADD:
			{
				NEXT_TOKEN;
				__pu_value *t = NULL;
				TERM(L, t);
                CHECK_EXP(t);
                __pu_value *r = NULL;
                MAKE_TEMP_VALUE(r);
                //r->SetType(NUM);
				*r = *temp + *t;
                temp = r;
			}
			break;
		case OPT_SUB:
			{
				NEXT_TOKEN;
				__pu_value *t = NULL;
				TERM(L,t);
                CHECK_EXP(t);
                __pu_value *r = NULL;
                MAKE_TEMP_VALUE(r);
                //r->SetType(NUM);
				*r = *temp - *t;
                temp = r;
			}
			break;
		default:
			return;
		}

		tp = TOKEN.type;
		nv = TOKEN.optype;
	}
}
Ejemplo n.º 17
0
void TERM_TAIL(void){
	/*term_tail = dotoperator term | epsilon*/
	switch(l){

	case '*': case '/': case AND:													DOTOPERATOR(); TERM(); return;
	case '+': case '-': case OR: case LSS: case GRT: case LEQ:
	case GEQ: case EQ: case NEQ: case ')': case ';': case ',':						return;
	default:																		StdError(__func__);
	}
}
Ejemplo n.º 18
0
void POSITIVESIMPEXPR_TAIL(void){
	/*positivesimpexpr_tail = operator positivesimpexpr_tail | epsilon*/
	switch(l){

	case OR: case '+': case '-':													OPERATOR(); TERM(); POSITIVESIMPEXPR_TAIL(); return;
	case LSS: case GRT: case LEQ: case GEQ: case EQ: case NEQ:
	case ')': case ';': case ',':													return;
	default:																		StdError(__func__);
	}
}
Ejemplo n.º 19
0
static void
findstats(Pos p, Ori o)
{
	/* Recalculate cross assert and score total at 'p'
	 */
	Pos	left, right;
	Word lword, rword;
	Node n;
	Edge e;
	int	s;

	lword.n = rword.n = 0;
	if(EDGE(p))
		return;

	/* find word to the left */
	s = 0;
	for(left=PREV(p,o); HASLETTER(left); left = PREV(left,o))
		;
	left = NEXT(left,o);
	while (HASLETTER(left)) {
		lword.c[lword.n++] = LETTER(left);
		s += SCORE(left);
		left = NEXT(left,o);
	}
	/* find word to the right */
	for(right=NEXT(p,o); HASLETTER(right); right = NEXT(right,o)) {
		rword.c[rword.n++] = LETTER(right);
		s += SCORE(right);
	}
	if(DBG) {
		wordprint(&lword);
		print("X");
		wordprint(&rword);
		print(" [%d] ", s);
	}

	SIDE(p,o) = s;
	ISANCHOR(p) = true;

	/* calculate cross asserts */
	CROSS(p,o) = 0;
	n = traverse(root, &lword, 0);
	assert(n>=0);
	if(n>0)
		do {
			e = dict[n++];
			if ( (rword.n && isword(NODE(e), &rword)) || 
				 (!rword.n && TERM(e)) ) {
				CROSS(p,o) |= 1 << LET(e);
				DPRINT("%c, ", LET(e)+'a');
			}
		} while (!(LAST(e)));
	DPRINT("\n");
}
Ejemplo n.º 20
0
ParseTree* Parser::TERM()
{
	ParseTree* retTree = new ParseTree();
	(*retTree).SetLeaf1(FACTOR()->getLeaf());
    
	if((*parser).get().token == MULTIPLY)
    {
		(*retTree).setAlt(2);
		if(!(*parser).Advance()) ThrowParserError();
		(*retTree).SetLeaf2(TERM()->getLeaf());
	}
    else
    {
		(*retTree).setAlt(1);
	}
	return retTree;
}
Ejemplo n.º 21
0
int		heredoc(t_group *grp, t_redir *curr)
{
	int		fd;

	TERM(other_read) = true;
	set_shell((~ICANON & ~ECHO));
	fd = open(TMP_FROM, O_WRONLY | O_APPEND | O_CREAT, 0644);
	while (grp->exit[1] == false)
	{
		TERM(cmd_line) = ft_strdup("");
		show_prompt(grp, "heredoc> ", 9, "\033[1;34m");
		read_cmd(grp, 0);
		if (ft_strcmp(TERM(cmd_line), curr->name) == 0)
			break ;
		ft_putendl_fd(TERM(cmd_line), fd);
		REMOVE(&TERM(cmd_line));
	}
	TERM(curs_pos) = 0;
	TERM(cmd_size) = 0;
	REMOVE(&TERM(cmd_line));
	TERM(other_read) = false;
	reset_shell();
	return (true);
}
Ejemplo n.º 22
0
size_t string::scan_move()
{
	if (scan_ptr_ == NULL)
		return 0;

	char *pEnd = acl_vstring_end(vbf_);
	if (scan_ptr_ >= pEnd)
	{
		clear();
		return 0;
	}

	size_t len = pEnd - scan_ptr_;
	acl_vstring_memmove(vbf_, scan_ptr_, len);
	TERM(vbf_);
	scan_ptr_  = STR(vbf_);

	return len;
}
Ejemplo n.º 23
0
size_t string::scan_buf(void* pbuf, size_t n, bool move /* = false */)
{
	if (pbuf == NULL || n == 0)
		return 0;

	const char *pEnd = buf_end();
	if (pEnd == NULL)
		return 0;

	size_t len = pEnd - scan_ptr_;
	if (len > n)
		len = n;
	memcpy(pbuf, scan_ptr_, len);
	if (move)
	{
		acl_vstring_memmove(vbf_, scan_ptr_, len);
		TERM(vbf_);
		scan_ptr_ = STR(vbf_);
	}
	else
		scan_ptr_ += len;
	return len;
}
Ejemplo n.º 24
0
void	init_search(t_group *grp)
{
	int	i;

	i = -1;
	if (grp->is_search == false)
	{
		TERM(line) = 0;
		grp->is_search = true;
		TERM(cmd_save) = SDUP(TERM(cmd_line));
		while (++i < TERM(curs_pos))
			if ((START_POS + i) % TERM(window->width) == 0)
				TERM(line) += 1;
		TERM(line) += 1;
		TERM(cmd_size) = 0;
		TERM(curs_pos) = 0;
		ft_bzero(TERM(cmd_line), LEN(TERM(cmd_line)));
		tputs(tgetstr("do", NULL), 0, ft_getchar);
		show_prompt(grp, "fsh: search: ", 13, "\033[1;34m");
	}
	else
		canceled_search(grp);
}
Ejemplo n.º 25
0
void term_draw() {
  unsigned char x, y;
  unsigned char i, j;
  write_index = 0;
  SDL_Rect srcrect;
  SDL_Rect dstrect;
  srcrect.w = 16;
  srcrect.h = 16;
  for( y = 0; y < term_h; y++ ) {
    for( x = 0; x < term_w; x++ ) {
      if( TERM( x, y ).tick < 15 ) {
        if( ++TERM( x,  y ).tick == 0 ) {
          TERM( x, y ).cchar = TERM( x,  y ).nchar;
          TERM( x, y ).cfont = TERM( x,  y ).nfont;
        }
      }

      if( TERM( x,  y ).cchar ) {
        dstrect.x = x << 4;
        dstrect.y = y << 4;
        srcrect.x = TERM( x,  y ).cchar << 4;
        srcrect.y = ( TERM( x,  y ).tick >= 0 ? TERM( x,  y ).tick << 4 : 240 );
        SDL_BlitSurface( font[ TERM( x,  y ).cfont ], &srcrect, screen, &dstrect );
      }
    }
  }
  if( cx >= 0 ) { 
    srcrect.x = 0;
    srcrect.y = ( ct > 15 ? 240 : ct << 4 );
    dstrect.x = cx << 4;
    dstrect.y = cy << 4;
    SDL_BlitSurface( font[ 0 ], &srcrect, screen, &dstrect );
    if( ++ct == 32 ) ct = 0;
  }
}
Ejemplo n.º 26
0
void		ft_go_end(t_group *grp)
{
	while (TERM(curs_pos) < TERM(cmd_size))
		ft_right_arrow(grp);
}
Ejemplo n.º 27
0
void		ft_go_home(t_group *grp)
{
	while (TERM(curs_pos) > 0)
		ft_left_arrow(grp);
}
Ejemplo n.º 28
0
/*******************************************************************************
 ***  FUNCTION MORETERM()
 *******************************************************************************
 ***  DESCRIPTION  :  Processes MORETERM grammar rule.
 ***
 ***  MORETERM -> ADDOP TERM MORETERM | e
 ***
 ******************************************************************************/
void RecursiveParser::MORETERM(TableEntry & rplace)
{
   TableEntry tplace;
   TableEntry temp;
   string code;

   if (global->Token == Global::addopt || global->Token == Global::relopt)
   {
      temp = tempVar();

      if (temp.depth <= 1)
         code += temp.Lexeme;

      else
      {
         code += "[bp-" + NumberToString(temp.var.Offset);
         code += "]";
      }

      code += " = ";

      if (rplace.TypeOfEntry == varEntry)
      {
         if (rplace.depth <= 1)
            code += rplace.Lexeme;

         else
         {
            code += "[bp";

            if (rplace.isParam)
               code += "+";
            else
               code += "-";

            code += NumberToString(rplace.var.Offset);
            code += "]";
         }
      }
      else if (rplace.TypeOfEntry == constEntry)
      {
         if (rplace.constant.TypeOfConstant == floatType)
            code += NumberToString(rplace.constant.ValueR);

         else
            code += NumberToString(rplace.constant.Value);
      }

      code += " " + global->Lexeme + " ";

      ADDOP();

      TERM(tplace);

      if (tplace.TypeOfEntry == varEntry)
      {
         if (tplace.depth <= 1)
            code += tplace.Lexeme;

         else
         {
            code += "[bp";

            if (tplace.isParam)
               code += "+";
            else
               code += "-";

            code += NumberToString(tplace.var.Offset);
            code += "]";
         }
      }
      else if (tplace.TypeOfEntry == constEntry)
      {
         if (tplace.constant.TypeOfConstant == floatType)
            code += NumberToString(tplace.constant.ValueR);

         else
            code += NumberToString(tplace.constant.Value);
      }

      rplace = temp;
      emit(code);
      MORETERM(rplace);
   }
}
Ejemplo n.º 29
0
static int test_TERM_CharSym(void)
{
  Lexer l;
  l.tok = (Token){.sym=0, .c=0};
  l.input.str = "a";
  l.input.wstr = NULL;
  l.index = 0;
  l.nbuf = 0;

  nextsym(&l);
  PTree *tree = TERM(&l);
  expect(Eof, &l);

  TA_PTR_NE(tree, NULL);
  TA_INT_EQ(tree->nt, TERMnt);
  TA_INT_EQ(tree->nchildren, 1);
  TA_INT_EQ(tree->children[0]->tok.sym, CharSym);
  TA_CHAR_EQ(tree->children[0]->tok.c, 'a');

  free_tree(tree);
  return 0;
}

static int test_TERM_Minus(void)
{
  Lexer l;
  l.tok = (Token){.sym=0, .c=0};
  l.input.str = "-";
  l.input.wstr = NULL;
  l.index = 0;
  l.nbuf = 0;

  nextsym(&l);
  PTree *tree = TERM(&l);
  expect(Eof, &l);

  TA_PTR_NE(tree, NULL);
  TA_INT_EQ(tree->nt, TERMnt);
  TA_INT_EQ(tree->nchildren, 1);
  TA_INT_EQ(tree->children[0]->tok.sym, Minus);
  TA_CHAR_EQ(tree->children[0]->tok.c, '-');

  free_tree(tree);
  return 0;
}

static int test_TERM_Caret(void)
{
  Lexer l;
  l.tok = (Token){.sym=0, .c=0};
  l.input.str = "^";
  l.input.wstr = NULL;
  l.index = 0;
  l.nbuf = 0;

  nextsym(&l);
  PTree *tree = TERM(&l);
  expect(Eof, &l);

  TA_PTR_NE(tree, NULL);
  TA_INT_EQ(tree->nt, TERMnt);
  TA_INT_EQ(tree->nchildren, 1);
  TA_INT_EQ(tree->children[0]->tok.sym, Caret);
  TA_CHAR_EQ(tree->children[0]->tok.c, '^');

  free_tree(tree);
  return 0;
}

static int test_TERM_Dot(void)
{
  Lexer l;
  l.tok = (Token){.sym=0, .c=0};
  l.input.str = ".";
  l.input.wstr = NULL;
  l.index = 0;
  l.nbuf = 0;

  nextsym(&l);
  PTree *tree = TERM(&l);
  expect(Eof, &l);

  TA_PTR_NE(tree, NULL);
  TA_INT_EQ(tree->nt, TERMnt);
  TA_INT_EQ(tree->nchildren, 1);
  TA_INT_EQ(tree->children[0]->tok.sym, Dot);
  TA_CHAR_EQ(tree->children[0]->tok.c, '.');

  free_tree(tree);
  return 0;
}

static int test_TERM_Special(void)
{
  Lexer l;
  l.tok = (Token){.sym=0, .c=0};
  l.input.str = "\\w";
  l.input.wstr = NULL;
  l.index = 0;
  l.nbuf = 0;

  nextsym(&l);
  PTree *tree = TERM(&l);
  expect(Eof, &l);

  TA_PTR_NE(tree, NULL);
  TA_INT_EQ(tree->nt, TERMnt);
  TA_INT_EQ(tree->nchildren, 1);
  TA_INT_EQ(tree->children[0]->tok.sym, Special);
  TA_CHAR_EQ(tree->children[0]->tok.c, 'w');

  free_tree(tree);
  return 0;
}

static int test_TERM_Subexpr(void)
{
  Lexer l;
  l.tok = (Token){.sym=0, .c=0};
  l.input.str = "(a+)";
  l.input.wstr = NULL;
  l.index = 0;
  l.nbuf = 0;

  nextsym(&l);
  PTree *tree = TERM(&l);
  expect(Eof, &l);

  TA_PTR_NE(tree, NULL);
  TA_INT_EQ(tree->nt, TERMnt);
  TA_INT_EQ(tree->nchildren, 3);
  TA_INT_EQ(tree->children[1]->nt, REGEXnt);

  free_tree(tree);
  return 0;
}

static int test_TERM_Class(void)
{
  Lexer l;
  l.tok = (Token){.sym=0, .c=0};
  l.input.str = "[abc]";
  l.input.wstr = NULL;
  l.index = 0;
  l.nbuf = 0;

  nextsym(&l);
  PTree *tree = TERM(&l);
  expect(Eof, &l);

  TA_PTR_NE(tree, NULL);
  TA_INT_EQ(tree->nt, TERMnt);
  TA_INT_EQ(tree->nchildren, 3);
  TA_INT_EQ(tree->production, 3);
  TA_INT_EQ(tree->children[1]->nt, CLASSnt);

  free_tree(tree);
  return 0;
}

static int test_TERM_NClass(void)
{
  Lexer l;
  l.tok = (Token){.sym=0, .c=0};
  l.input.str = "[^abc]";
  l.input.wstr = NULL;
  l.index = 0;
  l.nbuf = 0;

  nextsym(&l);
  PTree *tree = TERM(&l);
  expect(Eof, &l);

  TA_PTR_NE(tree, NULL);
  TA_INT_EQ(tree->nt, TERMnt);
  TA_INT_EQ(tree->nchildren, 3);
  TA_INT_EQ(tree->production, 4);
  TA_INT_EQ(tree->children[1]->nt, CLASSnt);

  free_tree(tree);
  return 0;
}

static int test_EXPR_Term(void)
{
  Lexer l;
  l.tok = (Token){.sym=0, .c=0};
  l.input.str = "a";
  l.input.wstr = NULL;
  l.index = 0;
  l.nbuf = 0;

  nextsym(&l);
  PTree *tree = EXPR(&l);
  expect(Eof, &l);

  TA_PTR_NE(tree, NULL);
  TA_INT_EQ(tree->nt, EXPRnt);
  TA_INT_EQ(tree->nchildren, 1);
  TA_INT_EQ(tree->children[0]->nt, TERMnt);
  TA_INT_EQ(tree->children[0]->nchildren, 1);

  free_tree(tree);
  return 0;
}

static int test_EXPR_Plus(void)
{
  Lexer l;
  l.tok = (Token){.sym=0, .c=0};
  l.input.str = "a+";
  l.input.wstr = NULL;
  l.index = 0;
  l.nbuf = 0;

  nextsym(&l);
  PTree *tree = EXPR(&l);
  expect(Eof, &l);

  TA_PTR_NE(tree, NULL);
  TA_INT_EQ(tree->nt, EXPRnt);
  TA_INT_EQ(tree->nchildren, 2);
  TA_INT_EQ(tree->children[0]->nt, TERMnt);
  TA_INT_EQ(tree->children[0]->nchildren, 1);
  TEST_ASSERT(tree->children[1]->tok.sym == Plus);

  free_tree(tree);
  return 0;
}

static int test_EXPR_PlusQuestion(void)
{
  Lexer l;
  l.tok = (Token){.sym=0, .c=0};
  l.input.str = "a+?";
  l.input.wstr = NULL;
  l.index = 0;
  l.nbuf = 0;

  nextsym(&l);
  PTree *tree = EXPR(&l);
  expect(Eof, &l);

  TA_PTR_NE(tree, NULL);
  TA_INT_EQ(tree->nt, EXPRnt);
  TA_INT_EQ(tree->nchildren, 3);
  TA_INT_EQ(tree->children[0]->nt, TERMnt);
  TA_INT_EQ(tree->children[0]->nchildren, 1);
  TEST_ASSERT(tree->children[1]->tok.sym == Plus);
  TEST_ASSERT(tree->children[2]->tok.sym == Question);

  free_tree(tree);
  return 0;
}

static int test_EXPR_Star(void)
{
  Lexer l;
  l.tok = (Token){.sym=0, .c=0};
  l.input.str = "a*";
  l.input.wstr = NULL;
  l.index = 0;
  l.nbuf = 0;

  nextsym(&l);
  PTree *tree = EXPR(&l);
  expect(Eof, &l);

  TA_PTR_NE(tree, NULL);
  TA_INT_EQ(tree->nt, EXPRnt);
  TA_INT_EQ(tree->nchildren, 2);
  TA_INT_EQ(tree->children[0]->nt, TERMnt);
  TA_INT_EQ(tree->children[0]->nchildren, 1);
  TEST_ASSERT(tree->children[1]->tok.sym == Star);

  free_tree(tree);
  return 0;
}

static int test_EXPR_StarQuestion(void)
{
  Lexer l;
  l.tok = (Token){.sym=0, .c=0};
  l.input.str = "a*?";
  l.input.wstr = NULL;
  l.index = 0;
  l.nbuf = 0;

  nextsym(&l);
  PTree *tree = EXPR(&l);
  expect(Eof, &l);

  TA_PTR_NE(tree, NULL);
  TA_INT_EQ(tree->nt, EXPRnt);
  TA_INT_EQ(tree->nchildren, 3);
  TA_INT_EQ(tree->children[0]->nt, TERMnt);
  TA_INT_EQ(tree->children[0]->nchildren, 1);
  TEST_ASSERT(tree->children[1]->tok.sym == Star);
  TEST_ASSERT(tree->children[2]->tok.sym == Question);

  free_tree(tree);
  return 0;
}

static int test_EXPR_Question(void)
{
  Lexer l;
  l.tok = (Token){.sym=0, .c=0};
  l.input.str = "a?";
  l.input.wstr = NULL;
  l.index = 0;
  l.nbuf = 0;

  nextsym(&l);
  PTree *tree = EXPR(&l);
  expect(Eof, &l);

  TA_PTR_NE(tree, NULL);
  TA_INT_EQ(tree->nt, EXPRnt);
  TA_INT_EQ(tree->nchildren, 2);
  TA_INT_EQ(tree->children[0]->nt, TERMnt);
  TA_INT_EQ(tree->children[0]->nchildren, 1);
  TEST_ASSERT(tree->children[1]->tok.sym == Question);

  free_tree(tree);
  return 0;
}

static int test_EXPR_QuestionQuestion(void)
{
  Lexer l;
  l.tok = (Token){.sym=0, .c=0};
  l.input.str = "a??";
  l.input.wstr = NULL;
  l.index = 0;
  l.nbuf = 0;

  nextsym(&l);
  PTree *tree = EXPR(&l);
  expect(Eof, &l);

  TA_PTR_NE(tree, NULL);
  TA_INT_EQ(tree->nt, EXPRnt);
  TA_INT_EQ(tree->nchildren, 3);
  TA_INT_EQ(tree->children[0]->nt, TERMnt);
  TA_INT_EQ(tree->children[0]->nchildren, 1);
  TEST_ASSERT(tree->children[1]->tok.sym == Question);
  TEST_ASSERT(tree->children[2]->tok.sym == Question);

  free_tree(tree);
  return 0;
}

static int test_SUB_Normal(void)
{
  Lexer l;
  l.tok = (Token){.sym=0, .c=0};
  l.input.str = "a";
  l.input.wstr = NULL;
  l.index = 0;
  l.nbuf = 0;

  nextsym(&l);
  PTree *tree = SUB(&l);
  expect(Eof, &l);

  TA_PTR_NE(tree, NULL);
  TA_INT_EQ(tree->nt, SUBnt);
  TA_INT_EQ(tree->nchildren, 1);
  TA_INT_EQ(tree->children[0]->nt, EXPRnt);
  TA_INT_EQ(tree->children[0]->nchildren, 1);
  free_tree(tree);
  return 0;
}

static int test_SUB_Concat(void)
{
  Lexer l;
  l.tok = (Token){.sym=0, .c=0};
  l.input.str = "ab";
  l.input.wstr = NULL;
  l.index = 0;
  l.nbuf = 0;

  nextsym(&l);
  PTree *tree = SUB(&l);
  expect(Eof, &l);

  TA_PTR_NE(tree, NULL);
  TA_INT_EQ(tree->nt, SUBnt);
  TA_INT_EQ(tree->nchildren, 2);
  TA_INT_EQ(tree->children[0]->nt, EXPRnt);
  TA_INT_EQ(tree->children[0]->nchildren, 1);
  TA_INT_EQ(tree->children[1]->nt, SUBnt);
  TA_INT_EQ(tree->children[1]->nchildren, 1);
  TA_INT_EQ(tree->children[1]->children[0]->nt, EXPRnt);

  free_tree(tree);
  return 0;
}

static int test_REGEX_Normal(void)
{
  Lexer l;
  l.tok = (Token){.sym=0, .c=0};
  l.input.str = "a";
  l.input.wstr = NULL;
  l.index = 0;
  l.nbuf = 0;

  nextsym(&l);
  PTree *tree = REGEX(&l);
  expect(Eof, &l);

  TA_PTR_NE(tree, NULL);
  TA_INT_EQ(tree->nt, REGEXnt);
  TA_INT_EQ(tree->nchildren, 1);
  TA_INT_EQ(tree->children[0]->nt, SUBnt);
  TA_INT_EQ(tree->children[0]->nchildren, 1);

  free_tree(tree);
  return 0;
}

static int test_REGEX_Alternate(void)
{
  Lexer l;
  l.tok = (Token){.sym=0, .c=0};
  l.input.str = "a|b";
  l.input.wstr = NULL;
  l.index = 0;
  l.nbuf = 0;

  nextsym(&l);
  PTree *tree = REGEX(&l);
  expect(Eof, &l);

  TA_PTR_NE(tree, NULL);
  TA_INT_EQ(tree->nt, REGEXnt);
  TA_INT_EQ(tree->nchildren, 3);
  TA_INT_EQ(tree->children[0]->nt, SUBnt);
  TA_INT_EQ(tree->children[0]->nchildren, 1);
  TA_INT_EQ(tree->children[1]->tok.sym, Pipe);
  TA_INT_EQ(tree->children[2]->nt, REGEXnt);
  TA_INT_EQ(tree->children[2]->nchildren, 1);
  TA_INT_EQ(tree->children[2]->children[0]->nt, SUBnt);

  free_tree(tree);
  return 0;
}

static int test_CLASS_range(void)
{
  Lexer l;
  l.tok = (Token){.sym=0, .c=0};
  l.input.str = "a-b";
  l.input.wstr = NULL;
  l.index = 0;
  l.nbuf = 0;

  nextsym(&l);
  PTree *tree = CLASS(&l);
  expect(Eof, &l);

  TA_PTR_NE(tree, NULL);
  TA_INT_EQ(tree->nt, CLASSnt);
  TA_INT_EQ(tree->nchildren, 2);
  TA_INT_EQ(tree->children[0]->tok.sym, CharSym);
  TA_CHAR_EQ(tree->children[0]->tok.c, 'a');
  TA_INT_EQ(tree->children[1]->tok.sym, CharSym);
  TA_CHAR_EQ(tree->children[1]->tok.c, 'b');

  free_tree(tree);
  return 0;
}

static int test_CLASS_range_range(void)
{
  Lexer l;
  l.tok = (Token){.sym=0, .c=0};
  l.input.str = "a-b1-2";
  l.input.wstr = NULL;
  l.index = 0;
  l.nbuf = 0;

  nextsym(&l);
  PTree *tree = CLASS(&l);
  expect(Eof, &l);

  TA_PTR_NE(tree, NULL);
  TA_INT_EQ(tree->nt, CLASSnt);
  TA_INT_EQ(tree->nchildren, 3);
  TA_INT_EQ(tree->children[0]->tok.sym, CharSym);
  TA_CHAR_EQ(tree->children[0]->tok.c, 'a');
  TA_INT_EQ(tree->children[1]->tok.sym, CharSym);
  TA_CHAR_EQ(tree->children[1]->tok.c, 'b');
  TA_INT_EQ(tree->children[2]->nt, CLASSnt);
  TA_INT_EQ(tree->children[2]->nchildren, 2);
  TA_INT_EQ(tree->children[2]->children[0]->tok.sym, CharSym);
  TA_CHAR_EQ(tree->children[2]->children[0]->tok.c, '1');
  TA_INT_EQ(tree->children[2]->children[1]->tok.sym, CharSym);
  TA_CHAR_EQ(tree->children[2]->children[1]->tok.c, '2');

  free_tree(tree);
  return 0;
}

static int test_CLASS_single(void)
{
  char *accept[] = {".", "+", "*", "?", "(", ")", "|"};
  for (size_t i = 0; i < nelem(accept); i++) {
    Lexer l;
    l.tok = (Token){.sym=0, .c=0};
    l.input.str = accept[i];
    l.input.wstr = NULL;
    l.index = 0;
    l.nbuf = 0;

    nextsym(&l);
    PTree *tree = CLASS(&l);
    expect(Eof, &l);

    TA_PTR_NE(tree, NULL);
    TA_INT_EQ(tree->nt, CLASSnt);
    TA_INT_EQ(tree->nchildren, 1);
    TA_INT_EQ(tree->children[0]->tok.sym, CharSym);
    TA_INT_EQ(tree->children[0]->tok.c, accept[i][0]);

    free_tree(tree);
  }
  return 0;
}

static int test_CLASS_single_hyphen(void)
{
  Lexer l;
  l.tok = (Token){.sym=0, .c=0};
  l.input.str = "a-";
  l.input.wstr = NULL;
  l.index = 0;
  l.nbuf = 0;

  nextsym(&l);
  PTree *tree = CLASS(&l);
  expect(Eof, &l);

  TA_PTR_NE(tree, NULL);
  TA_INT_EQ(tree->nt, CLASSnt);
  TA_INT_EQ(tree->nchildren, 2);
  TA_INT_EQ(tree->children[0]->tok.sym, CharSym);
  TA_CHAR_EQ(tree->children[0]->tok.c, 'a');
  TA_INT_EQ(tree->children[1]->nt, CLASSnt);
  TA_INT_EQ(tree->children[1]->nchildren, 1);
  TA_INT_EQ(tree->children[1]->children[0]->tok.sym, Minus);

  free_tree(tree);
  return 0;
}

static int test_reparse(void)
{
  PTree *tree = reparse("a+|b*");

  TA_PTR_NE(tree, NULL);
  TA_INT_EQ(tree->nt, REGEXnt);
  TA_INT_EQ(tree->nchildren, 3);
  TA_INT_EQ(tree->children[0]->nt, SUBnt);
  TA_INT_EQ(tree->children[0]->nchildren, 1);
  TA_INT_EQ(tree->children[0]->children[0]->nt, EXPRnt);
  TA_INT_EQ(tree->children[0]->children[0]->nchildren, 2);
  TA_INT_EQ(tree->children[0]->children[0]->children[0]->nt, TERMnt);
  TA_INT_EQ(tree->children[0]->children[0]->children[0]->nchildren, 1);
  TA_INT_EQ(tree->children[0]->children[0]->children[0]->children[0]->tok.sym, CharSym);
  TA_CHAR_EQ(tree->children[0]->children[0]->children[0]->children[0]->tok.c, 'a');
  TA_INT_EQ(tree->children[0]->children[0]->children[1]->tok.sym, Plus);
  TA_INT_EQ(tree->children[1]->tok.sym, Pipe);
  TA_INT_EQ(tree->children[2]->nt, REGEXnt);
  TA_INT_EQ(tree->children[2]->nchildren, 1);
  TA_INT_EQ(tree->children[2]->children[0]->nt, SUBnt);
  TA_INT_EQ(tree->children[2]->children[0]->nchildren, 1);
  TA_INT_EQ(tree->children[2]->children[0]->children[0]->nt, EXPRnt);
  TA_INT_EQ(tree->children[2]->children[0]->children[0]->nchildren, 2);
  TA_INT_EQ(tree->children[2]->children[0]->children[0]->children[0]->nt, TERMnt);
  TA_INT_EQ(tree->children[2]->children[0]->children[0]->children[0]->nchildren, 1);
  TA_INT_EQ(tree->children[2]->children[0]->children[0]->children[0]->children[0]->tok.sym, CharSym);
  TA_CHAR_EQ(tree->children[2]->children[0]->children[0]->children[0]->children[0]->tok.c, 'b');
  TA_INT_EQ(tree->children[2]->children[0]->children[0]->children[1]->tok.sym, Star);

  free_tree(tree);
  return 0;
}
Ejemplo n.º 30
0
void term_white( unsigned char x, unsigned char y, unsigned char c ) {
  while( c-- ) {
    memset( &TERM( x++,  y ), 0, sizeof( termchar_t ) );
  }
}