2 * This file was ported to MPlayer from xine CVS asmrp.c,v 1.2 2002/12/17 16:49:48
6 * Copyright (C) 2002 the xine project
8 * This file is part of xine, a free video player.
10 * xine is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * xine is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
25 * a parser for real's asm rules
27 * grammar for these rules:
31 rule = ( '#' condition { ',' assignment } | [ assignment {',' assignment} ]) ';'
32 assignment = id '=' const
33 const = ( number | string )
34 condition = comp_expr { ( '&&' | '||' ) comp_expr }
35 comp_expr = operand { ( '<' | '<=' | '==' | '>=' | '>' ) operand }
36 operand = ( '$' id | num | '(' condition ')' )
48 #define ASMRP_SYM_NONE 0
49 #define ASMRP_SYM_EOF 1
51 #define ASMRP_SYM_NUM 2
52 #define ASMRP_SYM_ID 3
53 #define ASMRP_SYM_STRING 4
55 #define ASMRP_SYM_HASH 10
56 #define ASMRP_SYM_SEMICOLON 11
57 #define ASMRP_SYM_COMMA 12
58 #define ASMRP_SYM_EQUALS 13
59 #define ASMRP_SYM_AND 14
60 #define ASMRP_SYM_OR 15
61 #define ASMRP_SYM_LESS 16
62 #define ASMRP_SYM_LEQ 17
63 #define ASMRP_SYM_GEQ 18
64 #define ASMRP_SYM_GREATER 19
65 #define ASMRP_SYM_DOLLAR 20
66 #define ASMRP_SYM_LPAREN 21
67 #define ASMRP_SYM_RPAREN 22
69 #define ASMRP_MAX_ID 1024
71 #define ASMRP_MAX_SYMTAB 10
85 char str
[ASMRP_MAX_ID
];
93 asmrp_sym_t sym_tab
[ASMRP_MAX_SYMTAB
];
98 static asmrp_t
*asmrp_new (void) {
102 p
= malloc (sizeof (asmrp_t
));
105 p
->sym
= ASMRP_SYM_NONE
;
110 static void asmrp_dispose (asmrp_t
*p
) {
114 for (i
=0; i
<p
->sym_tab_num
; i
++)
115 free (p
->sym_tab
[i
].id
);
120 static void asmrp_getch (asmrp_t
*p
) {
121 p
->ch
= p
->buf
[p
->pos
];
125 printf ("%c\n", p
->ch
);
130 static void asmrp_init (asmrp_t
*p
, const char *str
) {
132 p
->buf
= strdup (str
);
138 static void asmrp_number (asmrp_t
*p
) {
143 while ( (p
->ch
>='0') && (p
->ch
<='9') ) {
145 num
= num
*10 + (p
->ch
- '0');
150 p
->sym
= ASMRP_SYM_NUM
;
154 static void asmrp_string (asmrp_t
*p
) {
160 while ( (p
->ch
!='"') && (p
->ch
>=32) ) {
172 p
->sym
= ASMRP_SYM_STRING
;
175 static void asmrp_identifier (asmrp_t
*p
) {
181 while ( ((p
->ch
>='A') && (p
->ch
<='z'))
182 || ((p
->ch
>='0') && (p
->ch
<='9'))) {
191 p
->sym
= ASMRP_SYM_ID
;
195 static void asmrp_print_sym (asmrp_t
*p
) {
210 printf ("NUM %d\n", p
->num
);
214 printf ("ID '%s'\n", p
->str
);
217 case ASMRP_SYM_STRING
:
218 printf ("STRING \"%s\"\n", p
->str
);
225 case ASMRP_SYM_SEMICOLON
:
228 case ASMRP_SYM_COMMA
:
231 case ASMRP_SYM_EQUALS
:
249 case ASMRP_SYM_GREATER
:
252 case ASMRP_SYM_DOLLAR
:
255 case ASMRP_SYM_LPAREN
:
258 case ASMRP_SYM_RPAREN
:
263 printf ("unknown symbol %d\n", p
->sym
);
268 static void asmrp_get_sym (asmrp_t
*p
) {
270 while (p
->ch
<= 32) {
272 p
->sym
= ASMRP_SYM_EOF
;
285 p
->sym
= ASMRP_SYM_HASH
;
289 p
->sym
= ASMRP_SYM_SEMICOLON
;
293 p
->sym
= ASMRP_SYM_COMMA
;
297 p
->sym
= ASMRP_SYM_EQUALS
;
303 p
->sym
= ASMRP_SYM_AND
;
309 p
->sym
= ASMRP_SYM_OR
;
315 p
->sym
= ASMRP_SYM_LESS
;
318 p
->sym
= ASMRP_SYM_LEQ
;
323 p
->sym
= ASMRP_SYM_GREATER
;
326 p
->sym
= ASMRP_SYM_GEQ
;
331 p
->sym
= ASMRP_SYM_DOLLAR
;
335 p
->sym
= ASMRP_SYM_LPAREN
;
339 p
->sym
= ASMRP_SYM_RPAREN
;
348 case '0': case '1': case '2': case '3': case '4':
349 case '5': case '6': case '7': case '8': case '9':
354 asmrp_identifier (p
);
363 static int asmrp_find_id (asmrp_t
*p
, char *s
) {
367 for (i
=0; i
<p
->sym_tab_num
; i
++) {
368 if (!strcmp (s
, p
->sym_tab
[i
].id
))
375 static int asmrp_set_id (asmrp_t
*p
, char *s
, int v
) {
379 i
= asmrp_find_id (p
, s
);
384 p
->sym_tab
[i
].id
= strdup (s
);
387 printf ("new symbol '%s'\n", s
);
395 printf ("symbol '%s' assigned %d\n", s
, v
);
401 static int asmrp_condition (asmrp_t
*p
) ;
403 static int asmrp_operand (asmrp_t
*p
) {
408 printf ("operand\n");
415 case ASMRP_SYM_DOLLAR
:
419 if (p
->sym
!= ASMRP_SYM_ID
) {
420 printf ("error: identifier expected.\n");
424 i
= asmrp_find_id (p
, p
->str
);
426 printf ("error: unknown identifier %s\n", p
->str
);
428 ret
= p
->sym_tab
[i
].v
;
439 case ASMRP_SYM_LPAREN
:
442 ret
= asmrp_condition (p
);
444 if (p
->sym
!= ASMRP_SYM_RPAREN
) {
445 printf ("error: ) expected.\n");
453 printf ("syntax error, $ number or ( expected\n");
458 printf ("operand done, =%d\n", ret
);
464 static int asmrp_comp_expression (asmrp_t
*p
) {
469 printf ("comp_expression\n");
472 a
= asmrp_operand (p
);
474 while ( (p
->sym
== ASMRP_SYM_LESS
)
475 || (p
->sym
== ASMRP_SYM_LEQ
)
476 || (p
->sym
== ASMRP_SYM_EQUALS
)
477 || (p
->sym
== ASMRP_SYM_GEQ
)
478 || (p
->sym
== ASMRP_SYM_GREATER
) ) {
484 b
= asmrp_operand (p
);
493 case ASMRP_SYM_EQUALS
:
499 case ASMRP_SYM_GREATER
:
507 printf ("comp_expression done = %d\n", a
);
512 static int asmrp_condition (asmrp_t
*p
) {
517 printf ("condition\n");
520 a
= asmrp_comp_expression (p
);
522 while ( (p
->sym
== ASMRP_SYM_AND
) || (p
->sym
== ASMRP_SYM_OR
) ) {
529 b
= asmrp_comp_expression (p
);
542 printf ("condition done = %d\n", a
);
547 static void asmrp_assignment (asmrp_t
*p
) {
550 printf ("assignment\n");
553 if (p
->sym
== ASMRP_SYM_COMMA
|| p
->sym
== ASMRP_SYM_SEMICOLON
) {
555 printf ("empty assignment\n");
560 if (p
->sym
!= ASMRP_SYM_ID
) {
561 printf ("error: identifier expected\n");
566 if (p
->sym
!= ASMRP_SYM_EQUALS
) {
567 printf ("error: = expected\n");
572 if ( (p
->sym
!= ASMRP_SYM_NUM
) && (p
->sym
!= ASMRP_SYM_STRING
)
573 && (p
->sym
!= ASMRP_SYM_ID
)) {
574 printf ("error: number or string expected\n");
580 printf ("assignment done\n");
584 static int asmrp_rule (asmrp_t
*p
) {
594 if (p
->sym
== ASMRP_SYM_HASH
) {
597 ret
= asmrp_condition (p
);
599 while (p
->sym
== ASMRP_SYM_COMMA
) {
603 asmrp_assignment (p
);
606 } else if (p
->sym
!= ASMRP_SYM_SEMICOLON
) {
608 asmrp_assignment (p
);
610 while (p
->sym
== ASMRP_SYM_COMMA
) {
613 asmrp_assignment (p
);
618 printf ("rule done = %d\n", ret
);
621 if (p
->sym
!= ASMRP_SYM_SEMICOLON
) {
622 printf ("semicolon expected.\n");
631 static int asmrp_eval (asmrp_t
*p
, int *matches
) {
633 int rule_num
, num_matches
;
641 rule_num
= 0; num_matches
= 0;
642 while (p
->sym
!= ASMRP_SYM_EOF
) {
644 if (asmrp_rule (p
)) {
646 printf ("rule #%d is true\n", rule_num
);
648 matches
[num_matches
] = rule_num
;
655 matches
[num_matches
] = -1;
659 int asmrp_match (const char *rules
, int bandwidth
, int *matches
) {
666 asmrp_init (p
, rules
);
668 asmrp_set_id (p
, "Bandwidth", bandwidth
);
669 asmrp_set_id (p
, "OldPNMPlayer", 0);
671 num_matches
= asmrp_eval (p
, matches
);