File: cf3lex.l

package info (click to toggle)
cfengine3 3.24.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,552 kB
  • sloc: ansic: 163,161; sh: 10,296; python: 2,950; makefile: 1,744; lex: 784; yacc: 633; perl: 211; pascal: 157; xml: 21; sed: 13
file content (784 lines) | stat: -rw-r--r-- 28,791 bytes parent folder | download | duplicates (2)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
%top{
/*
   Copyright 2021 Northern.tech AS

   This file is part of CFEngine 3 - written and maintained by Northern.tech AS.

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; version 3.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA

  To the extent this program is licensed as part of the Enterprise
  versions of CFEngine, the applicable Commercial Open Source License
  (COSL) may apply to this file if you as a licensee so wish it. See
  included file COSL.txt.
*/

/*******************************************************************/
/*                                                                 */
/*  LEXER for cfengine 3                                           */
/*                                                                 */
/*******************************************************************/

#include <platform.h>

#include <cf3parse.h>
#include <parser_state.h>
#include <file_lib.h>
#include <regex.h>
#include <feature.h>
#include <version_comparison.h>
}

%{
/* yyinput/input are generated and unused */

#if defined(__GNUC__)
# define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
# if GCC_VERSION >= 40200
#  pragma GCC diagnostic ignored "-Wunused-function"
# endif
#endif

#undef malloc
#undef realloc
#define malloc xmalloc
#define realloc xrealloc

//#define ParserDebug if (LogGetGlobalLevel() >= LOG_LEVEL_DEBUG) printf
#define ParserDebug(...) ((void) 0)
#define P PARSER_STATE

static Regex *context_expression_whitespace_rx = NULL;

static int DeEscapeQuotedString(const char *from, char *to);

int yywrap(void)
{
return 1;
}

static void yyuseraction()
{
P.offsets.current += yyleng;
}

#define YY_USER_ACTION yyuseraction();

// Do not use lex - flex only

%}

%x if_ignore_state

space      [ \t]+

newline    ([\n]|[\xd][\xa])

eat_line  ([\n]|[\xd][\xa]).*

comment    #[^\n]*

macro_if_minimum_version  @if\ minimum_version\([0-9]{1,5}(\.[0-9]{1,5}){0,2}\)
macro_if_maximum_version  @if\ maximum_version\([0-9]{1,5}(\.[0-9]{1,5}){0,2}\)
macro_if_before_version   @if\ before_version\([0-9]{1,5}(\.[0-9]{1,5}){0,2}\)
macro_if_at_version       @if\ at_version\([0-9]{1,5}(\.[0-9]{1,5}){0,2}\)
macro_if_after_version    @if\ after_version\([0-9]{1,5}(\.[0-9]{1,5}){0,2}\)
macro_if_between_versions @if\ between_versions\([0-9]{1,5}(\.[0-9]{1,5}){0,2}\ *,\ *[0-9]{1,5}(\.[0-9]{1,5}){0,2}\)
macro_if_feature          @if\ feature\([a-zA-Z0-9_]+\)
macro_else  @else
macro_endif @endif
macro_line [^@\n\xd\xa].*

bundle     bundle

body       body

promise    promise

nakedvar   [$@][(][a-zA-Z0-9_\[\]\200-\377.:]+[)]|[$@][{][a-zA-Z0-9_\[\]\200-\377.:]+[}]|[$@][(][a-zA-Z0-9_\200-\377.:]+[\[][a-zA-Z0-9_$(){}\200-\377.:]+[\]]+[)]|[$@][{][a-zA-Z0-9_\200-\377.:]+[\[][a-zA-Z0-9_$(){}\200-\377.:]+[\]]+[}]

identifier [a-zA-Z0-9_\200-\377]+

symbol     [a-zA-Z0-9_\200-\377]+[:][a-zA-Z0-9_\200-\377]+

fat_arrow  =>

thin_arrow ->

/*
 * Three types of quoted strings:
 *
 * - string in double quotes, starts with double quote, runs until another
 *   double quote, \" masks the double quote.
 * - string in single quotes, starts with single quote, runs until another
 *   single quote, \' masks the single quote.
 * - string in backquotes, starts with backquote, runs until another backquote.
 *
 * The same rule formatted for the better readability:
 *
 * <qstring> := \" <dq> \" | \' <sq> \' | ` <bq> `
 * <dq> = <dqs>*
 * <dqs> = \\ <any> | [^"\\]
 * <sq> = <sqs>*
 * <sqs> = \\ <any> | [^'\\]
 * <bq> = <bqs>*
 * <bqs> = [^`]
 * <any> = . | \n
 *
 */

qstring        \"((\\(.|\n))|[^"\\])*\"|\'((\\(.|\n))|[^'\\])*\'|`[^`]*`

class          [.|&!()a-zA-Z0-9_\200-\377:][\t .|&!()a-zA-Z0-9_\200-\377:]*::
varclass       (\"[^"\0]*\"|\'[^'\0]*\')::

promise_guard  [a-zA-Z_]+:

%%
{eat_line}             {
                          free(P.current_line);
                          P.current_line = xstrdup(yytext+1);
                          ParserDebug("L:line %s\n", P.current_line);
                          P.line_no++;
                          P.line_pos = 1;
                          // push back on stack and skip first char
                          yyless(1);
                      }

{macro_if_minimum_version} {
                        if (  P.line_pos != 1 )
                        {
                             yyerror("fatal: macro @if must be at beginning of the line.");
                             return 0;
                        }
                        if (P.if_depth > 0)
                        {
                          yyerror("fatal: nested @if macros are not allowed");
                          return 0;
                        }

                        P.if_depth++;

                        const char* minimum = yytext+20;
                        ParserDebug("\tL:macro @if %d:version=%s\n", P.line_pos, minimum);

                        VersionComparison result = CompareVersion(Version(), minimum);
                        if (result == VERSION_GREATER || result == VERSION_EQUAL)
                        {
                            ParserDebug("\tL:macro @if %d:accepted to next @endif\n", P.line_pos);
                        }
                        else if (result == VERSION_SMALLER)
                        {
                            ParserDebug("\tL:macro @if %d:ignoring to next @endif or EOF\n", P.line_pos);
                            BEGIN(if_ignore_state);
                        }
                        else
                        {
                            assert(result == VERSION_ERROR);
                            yyerror("fatal: macro @if requested an unparseable version");
                        }
                      }

{macro_if_maximum_version} {
                        if (  P.line_pos != 1 )
                        {
                             yyerror("fatal: macro @if must be at beginning of the line.");
                             return 0;
                        }
                        if (P.if_depth > 0)
                        {
                          yyerror("fatal: nested @if macros are not allowed");
                          return 0;
                        }

                        P.if_depth++;

                        const char* maximum = yytext+20;
                        ParserDebug("\tL:macro @if %d:version=%s\n", P.line_pos, maximum);

                        VersionComparison result = CompareVersion(Version(), maximum);
                        if (result == VERSION_SMALLER || result == VERSION_EQUAL)
                        {
                            ParserDebug("\tL:macro @if %d:accepted to next @endif\n", P.line_pos);
                        }
                        else if (result == VERSION_GREATER)
                        {
                            ParserDebug("\tL:macro @if %d:ignoring to next @endif or EOF\n", P.line_pos);
                            BEGIN(if_ignore_state);
                        }
                        else
                        {
                            assert(result == VERSION_ERROR);
                            yyerror("fatal: macro @if requested an unparseable version");
                        }
                      }

{macro_if_before_version} {
                        if (  P.line_pos != 1 )
                        {
                             yyerror("fatal: macro @if must be at beginning of the line.");
                             return 0;
                        }
                        if (P.if_depth > 0)
                        {
                          yyerror("fatal: nested @if macros are not allowed");
                          return 0;
                        }

                        P.if_depth++;

                        const char* target = yytext + strlen("@if before_version(");
                        ParserDebug("\tL:macro @if %d:version=%s\n", P.line_pos, target);

                        VersionComparison result = CompareVersion(Version(), target);
                        if (result == VERSION_SMALLER)
                        {
                            ParserDebug("\tL:macro @if %d:accepted to next @endif\n", P.line_pos);
                        }
                        else if (result == VERSION_GREATER || result == VERSION_EQUAL)
                        {
                            ParserDebug("\tL:macro @if %d:ignoring to next @endif or EOF\n", P.line_pos);
                            BEGIN(if_ignore_state);
                        }
                        else
                        {
                            assert(result == VERSION_ERROR);
                            yyerror("fatal: macro @if requested an unparseable version");
                        }
                      }

{macro_if_at_version} {
                        if (  P.line_pos != 1 )
                        {
                             yyerror("fatal: macro @if must be at beginning of the line.");
                             return 0;
                        }
                        if (P.if_depth > 0)
                        {
                          yyerror("fatal: nested @if macros are not allowed");
                          return 0;
                        }

                        P.if_depth++;

                        const char* target = yytext + strlen("@if at_version(");
                        ParserDebug("\tL:macro @if %d:version=%s\n", P.line_pos, target);

                        VersionComparison result = CompareVersion(Version(), target);
                        if (result == VERSION_EQUAL)
                        {
                            ParserDebug("\tL:macro @if %d:accepted to next @endif\n", P.line_pos);
                        }
                        else if (result == VERSION_GREATER || result == VERSION_SMALLER)
                        {
                            ParserDebug("\tL:macro @if %d:ignoring to next @endif or EOF\n", P.line_pos);
                            BEGIN(if_ignore_state);
                        }
                        else
                        {
                            assert(result == VERSION_ERROR);
                            yyerror("fatal: macro @if requested an unparseable version");
                        }
                      }

{macro_if_after_version} {
                        if (  P.line_pos != 1 )
                        {
                             yyerror("fatal: macro @if must be at beginning of the line.");
                             return 0;
                        }
                        if (P.if_depth > 0)
                        {
                          yyerror("fatal: nested @if macros are not allowed");
                          return 0;
                        }

                        P.if_depth++;

                        const char* target = yytext + strlen("@if after_version(");
                        ParserDebug("\tL:macro @if %d:version=%s\n", P.line_pos, target);

                        VersionComparison result = CompareVersion(Version(), target);
                        if (result == VERSION_GREATER)
                        {
                            ParserDebug("\tL:macro @if %d:accepted to next @endif\n", P.line_pos);
                        }
                        else if (result == VERSION_EQUAL || result == VERSION_SMALLER)
                        {
                            ParserDebug("\tL:macro @if %d:ignoring to next @endif or EOF\n", P.line_pos);
                            BEGIN(if_ignore_state);
                        }
                        else
                        {
                            assert(result == VERSION_ERROR);
                            yyerror("fatal: macro @if requested an unparseable version");
                        }
                      }

{macro_if_between_versions} {
                        if ( P.line_pos != 1 )
                        {
                             yyerror("fatal: macro @if must be at beginning of the line.");
                             return 0;
                        }
                        if (P.if_depth > 0)
                        {
                          yyerror("fatal: nested @if macros are not allowed");
                          return 0;
                        }

                        P.if_depth++;

                        const char* from = yytext + strlen("@if between_versions(");

                        const char *to = strchr(from, ',');
                        to += 1;
                        while (*to == ' ')
                        {
                            to += 1;
                        }

                        ParserDebug("\tL:macro @if %d:between_versions(%s\n", P.line_pos, from);

                        VersionComparison a = CompareVersion(Version(), from);
                        VersionComparison b = CompareVersion(Version(), to);
                        if ((a == VERSION_EQUAL || a == VERSION_GREATER)
                            && (b == VERSION_EQUAL || b == VERSION_SMALLER))
                        {
                            ParserDebug("\tL:macro @if %d:accepted to next @endif\n", P.line_pos);
                        }
                        else
                        {
                            ParserDebug("\tL:macro @if %d:ignoring to next @endif or EOF\n", P.line_pos);
                            BEGIN(if_ignore_state);
                        }
                      }

{macro_if_feature}    {
                        if (  P.line_pos != 1 )
                        {
                          yyerror("fatal: macro @if must be at beginning of the line.");
                          return 0;
                        }

                        char* feature_text = yytext+12;
                        // remove trailing ')'
                        feature_text[strlen(feature_text)-1] = 0;
                        ParserDebug("\tL:macro @if %d:feature=%s\n", P.line_pos, feature_text);
                        {
                          if (P.if_depth > 0)
                          {
                            yyerror("fatal: nested @if macros are not allowed");
                            return 0;
                          }

                          P.if_depth++;

                          if (KnownFeature(feature_text))
                          {
                            ParserDebug("\tL:macro @if %d:accepted to next @endif\n", P.line_pos);
                          }
                          else
                          {
                            /* ignore to the next @endif */
                            ParserDebug("\tL:macro @if %d:ignoring to next @endif or EOF\n", P.line_pos);
                            BEGIN(if_ignore_state);
                          }
                        }
                      }

{macro_endif}         {
                        if (  P.line_pos != 1 )
                        {
                          yyerror("fatal: macro @endif must be at beginning of the line.");
                          return 0;
                        }

                        ParserDebug("\tL:macro @endif %d\n", P.line_pos);
                        BEGIN(INITIAL);
                        if (P.if_depth <= 0)
                        {
                          yyerror("fatal: @endif macros without a matching @if are not allowed");
                          return 0;
                        }
                        P.if_depth--;
                      }


<if_ignore_state>{macro_line}  {
                                  ParserDebug("\tL:inside macro @if, ignoring line text:\"%s\"\n", yytext);
                               }

<if_ignore_state>{macro_else} {
                                ParserDebug("\tL:macro @else, will no longer ignore lines\n", P.line_pos);
                                BEGIN(INITIAL);
                              }
{macro_else}                  {
                                if (P.if_depth <= 0)
                                {
                                  yyerror("fatal: @else macro without a matching @if are not allowed");
                                  return 0;
                                }
                                ParserDebug("\tL:macro @else, will now ignore lines\n", P.line_pos);
                                BEGIN(if_ignore_state);
                              }

<if_ignore_state>{macro_endif} {
                                 ParserDebug("\tL:macro @endif %d\n", P.line_pos);
                                 BEGIN(INITIAL);
                                 if (P.if_depth <= 0)
                                 {
                                   yyerror("fatal: @endif macros without a matching @if are not allowed");
                                   return 0;
                                 }
                                 P.if_depth--;
                               }

<if_ignore_state>{newline} {
                           }

<if_ignore_state>.    {
                          /* eat up al unknown chars when line starts with @*/
                          ParserDebug("\tL:inside macro @if, ignoring char text:\"%s\"\n", yytext);
                      }

{bundle}              {
                          /* Note this has to come before "id" since it is a subset of id */

                          if (P.currentclasses != NULL)
                          {
                              free(P.currentclasses);
                              P.currentclasses = NULL;
                          }

                          if (P.currentvarclasses != NULL)
                          {
                              free(P.currentvarclasses);
                              P.currentvarclasses = NULL;
                          }

                          P.line_pos += yyleng;
                          ParserDebug("\tL:bundle %d\n", P.line_pos);
                          return BUNDLE;
                      }

{body}                {
                          /* Note this has to come before "id" since it is a subset of id */

                          if (P.currentclasses != NULL)
                          {
                              free(P.currentclasses);
                              P.currentclasses = NULL;
                          }

                          if (P.currentvarclasses != NULL)
                          {
                              free(P.currentvarclasses);
                              P.currentvarclasses = NULL;
                          }

                          P.line_pos += yyleng;
                          ParserDebug("\tL:body %d\n", P.line_pos);
                          return BODY;
                      }

{promise}             {
                          /* Note this has to come before "id" since it is a subset of id */

                          if (P.currentclasses != NULL)
                          {
                              free(P.currentclasses);
                              P.currentclasses = NULL;
                          }

                          if (P.currentvarclasses != NULL)
                          {
                              free(P.currentvarclasses);
                              P.currentvarclasses = NULL;
                          }

                          P.line_pos += yyleng;
                          ParserDebug("\tL:promise %d\n", P.line_pos);
                          return PROMISE;
                      }

{identifier}          {
                          P.offsets.last_id = P.offsets.current - yyleng;
                          P.line_pos += yyleng;
                          ParserDebug("\tL:id %s %d\n", yytext, P.line_pos);
                          if (yyleng  > CF_MAXVARSIZE-1)
                          {
                              yyerror("identifier too long");
                          }
                          strncpy(P.currentid, yytext, CF_MAXVARSIZE - 1);
                          return IDENTIFIER;
                      }


{symbol}              {
                          P.offsets.last_id = P.offsets.current - yyleng;
                          P.line_pos += yyleng;
                          ParserDebug("\tL:symbol %s %d\n", yytext, P.line_pos);
                          if (yyleng > CF_MAXVARSIZE-1)
                          {
                              yyerror("qualified identifier too long");
                          }
                          strncpy(P.currentid, yytext, CF_MAXVARSIZE - 1);
                          return IDENTIFIER;
                      }


{fat_arrow}           {
                          P.line_pos += yyleng;
                          ParserDebug("\tL:assign %d\n", P.line_pos);
                          return FAT_ARROW;
                      }

{thin_arrow}          {
                          P.line_pos += yyleng;
                          ParserDebug("\tL:arrow %d\n", P.line_pos);
                          return THIN_ARROW;
                      }

{varclass}            {
                          char *tmp = NULL;

                          P.line_pos += yyleng;
                          ParserDebug("\tL:varclass %s %d\n", yytext, P.line_pos);

                          tmp = xstrdup(yytext+1); // remove leading quote
                          tmp[yyleng-4] = '\0'; // remove tail quote plus ::

                          if (P.currentclasses != NULL)
                          {
                              free(P.currentclasses);
                              P.currentclasses = NULL;
                          }

                          if (P.currentvarclasses != NULL)
                          {
                              free(P.currentvarclasses);
                              P.currentvarclasses = NULL;
                          }

                          P.currentvarclasses = xstrdup(tmp);

                          free(tmp);
                          return CLASS_GUARD;
                      }

{class}               {
                          char *tmp = NULL;

                          P.line_pos += yyleng;
                          ParserDebug("\tL:class %s %d\n", yytext, P.line_pos);
                          if (context_expression_whitespace_rx == NULL)
                          {
                              context_expression_whitespace_rx = CompileRegex(CFENGINE_REGEX_WHITESPACE_IN_CONTEXTS);
                          }

                          if (context_expression_whitespace_rx == NULL)
                          {
                              yyerror("The context expression whitespace regular expression could not be compiled, aborting.");
                          }

                          if (StringMatchFullWithPrecompiledRegex(context_expression_whitespace_rx, yytext))
                          {
                              yyerror("class names can't be separated by whitespace without an intervening operator");
                          }

                          tmp = xstrdup(yytext);
                          tmp[yyleng-2] = '\0';

                          if (P.currentclasses != NULL)
                          {
                              free(P.currentclasses);
                              P.currentclasses = NULL;
                          }

                          if (P.currentvarclasses != NULL)
                          {
                              free(P.currentvarclasses);
                              P.currentvarclasses = NULL;
                          }

                          P.currentclasses = xstrdup(tmp);

                          free(tmp);
                          return CLASS_GUARD;
                      }

{promise_guard}       {
                          char *tmp = NULL;

                          P.line_pos += yyleng;
                          ParserDebug("\tL:promise_guard %s %d\n", yytext, P.line_pos);
                          P.offsets.last_promise_guard_id = P.offsets.current - yyleng;

                          tmp = xstrdup(yytext);
                          assert(tmp[yyleng - 1] == ':');
                          tmp[yyleng - 1] = '\0'; // Exclude trailing colon in promise guard
                          assert(strlen(tmp) > 0);
                          assert(tmp[strlen(tmp) - 1] != ':');

                          strncpy(P.currenttype, tmp, CF_MAXVARSIZE - 1);

                          if (P.currentclasses != NULL)
                          {
                              free(P.currentclasses);
                              P.currentclasses = NULL;
                          }

                          if (P.currentvarclasses != NULL)
                          {
                              free(P.currentvarclasses);
                              P.currentvarclasses = NULL;
                          }

                          free(tmp);
                          return PROMISE_GUARD;
                      }

{qstring}             {
                          char *tmp = NULL;
                          int less = 0;

                          P.offsets.last_string = P.offsets.current - yyleng;
                          P.line_pos += yyleng;
                          ParserDebug("\tL:qstring %s %d\n", yytext, P.line_pos);

                          for (char *c = yytext; *c; ++c)
                          {
                              if (*c == '\n')
                              {
                                  P.line_no++;
                              }
                          }

                          tmp = xmalloc(yyleng + 1);

                          if ((less = DeEscapeQuotedString(yytext,tmp)) > 0)
                          {
                              yyless(less);
                              P.offsets.current -= less;
                          }

                          if (P.currentstring)
                          {
                              free(P.currentstring);
                          }

                          P.currentstring = xstrdup(tmp);

                          free(tmp);
                          return QUOTED_STRING;
                      }


{nakedvar}            {
                          P.line_pos += yyleng;
                          ParserDebug("\tL: %s %d\n", yytext, P.line_pos);
                          if (P.currentstring)
                          {
                              free(P.currentstring);
                          }
                          P.currentstring = xstrdup(yytext);
                          return NAKEDVAR;
                      }

{space}+              {
                          P.line_pos += yyleng;
                      }

{comment}             {
                      }


.                     {
                          P.line_pos++;
                          return yytext[0];
                      }

<if_ignore_state><<EOF>>     {
                               if (P.if_depth > 0)
                               {
                                 yyerror("EOF seen while @if was waiting for @endif in ignore_state");
                                 return 0;
                               }
                             }

<<EOF>>     {
              if (P.if_depth > 0)
              {
                yyerror("EOF seen while @if was waiting for @endif without ignore_state");
              }
              return 0; // loops forever without this
            }

%%

static int DeEscapeQuotedString(const char *from, char *to)
{
    char *cp;
    const char *sp;
    char start = *from;
    int len = strlen(from);

    if (len == 0)
    {
        return 0;
    }

    for (sp = from + 1, cp = to; (sp - from) < len; sp++, cp++)
    {
        if ((*sp == start))
        {
            *(cp) = '\0';

            if (*(sp + 1) != '\0')
            {
                return (2 + (sp - from));
            }

            return 0;
        }

      if (*sp == '\\')
      {
            switch (*(sp + 1))
            {
            case '\n':
                sp += 2;
                break;

            case ' ':
                break;

            case '\\':
            case '\"':
            case '\'':
                sp++;
                break;
            }
        }

        *cp = *sp;
    }

    yyerror("Runaway string");
    *(cp) = '\0';
    return 0;
}


/* EOF */