summaryrefslogtreecommitdiffstats
path: root/filters/kword/latex/import/parser/texparser.lex
blob: eb9b27aac8124dae4afe4fa772818d1c6e00fd35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * Ajouter une pile contenant tout les environnements.
 *
 * To compil :
 * flex -+ -otexparser.cc texparser.lex
 * g++ -o texparser texparser.cc -lfl -lm
 *
 */

%{
#include <stdio.h>
#include <string.h>
#include <sys/param.h>

#include "stack.h"

/* Stack handling */
void pop(int);
void push(int);

int ind = 0;

%}

%x MATHMODE ENV

commande \\[a-zA-Z]+{param}?{option}?{group}?

group \{[^\{]*\}

option \[.*\]

param \(.*\)

b_math \\\(
e_math \\\)
math \$

b_env \\begin\{{letter}+\}
e_env \\end\{{letter}+\}

display \$\$

comments \%.*\n

ws [ \n\t]
space ({ws}|\~|\\space)
texte [^\\\n\$\%]+

symbol ("$"("\\"{atoz}+|.)"$"|"\\#"|"\\$"|"\\%"|"\\ref")
letter [A-Za-z]

%%
<INITIAL,ENV>{b_env} {
	printf("Entre dans un env. : %s\n", yytext);
	push(ENV);
	BEGIN(ENV);
}

<ENV>{e_env} {
	printf("Sort d'un env.\n");
	pop(ENV);
	if(stackp == 0)
		BEGIN(INITIAL);
}

<INITIAL,ENV>{commande} {
	printf("Commande : %s\n", yytext);
	printf("%d\n", ind);
	ind++;
}

<INITIAL,ENV>"\n" {
	printf("Nouvelle ligne\n");
}

<MATHMODE>{math} {
	printf("Sort du mode math\n");
	pop(MATH);
	BEGIN(INITIAL);
}

<INITIAL,ENV>{math} {
	printf("Entre dans le mode math\n");
	push(MATH);
	BEGIN(MATHMODE);
}


<INITIAL,ENV>{comments} {
	printf("commentaire : %s\n", yytext);
}

<INITIAL,ENV>{texte} {
	printf("texte : %s\n", yytext);
}

<MATHMODE>{texte} {
	printf("texte mathematique : %s\n", yytext);
}

%%


void push(int name)
{
	if(stackp == 0)
	{
		fprintf(stdout, "init. stack!\n");
		stack = (Stack *) malloc(stack_size * sizeof(Stack));
	}
    if ( stackp == stack_size )
	{
		/* extend stack */
		stack_size *= 2;
		stack = (Stack *) realloc(stack, stack_size * sizeof(Stack));
		if ( stack == NULL )
		{
			fprintf(stderr, "texparser: stack out of memory");
			exit(3);
	    }
		fprintf(stdout, "%d", stack);
    }
    
    /*if ( (stack[stackp].name =
		(int *) malloc(strlen(name) + 1)) == NULL )
	{
		fprintf(stderr, "texparser: out of memory\n");
		exit(3);
    }*/

    stack[stackp].name = name;
	fprintf(stdout, "type added in stack : %d\n", name);
    ++stackp;
}


void pop(int name)
{
    if ( stack == 0 )
    {
       	fprintf(stderr, "texparser: Stack underflow\n");
		exit(4);
    }

	if(stack[stackp - 1].name == name)
	{
    	--stackp;
	    //free(stack[stackp].name);
	}
	else
	{
		fprintf(stderr, "texparser : Bad env.\n");
	}
}