1 %{
2 /*
3  * libiio - Library for interfacing industrial I/O (IIO) devices
4  *
5  * Copyright (C) 2014 Analog Devices, Inc.
6  * Author: Paul Cercueil <paul.cercueil@analog.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * */
19 
20 #include "parser.h"
21 #include "ops.h"
22 
23 #include <string.h>
24 %}
25 
26 %option noyywrap reentrant bison-bridge nounistd nounput noinput
27 
28 WORD (([[:alpha:]]+,)|(iio:))?(-|_|\.|[[:alnum:]])+
29 
30 %s WANT_DEVICE
31 %s WANT_CHN_OR_ATTR
32 %s WANT_CHN
33 %s WANT_ATTR
34 %s WANT_VALUE
35 %%
36 
37 <INITIAL>VERSION|version {
38 	return VERSION;
39 }
40 
41 <INITIAL>PRINT|print {
42 	return PRINT;
43 }
44 
45 <INITIAL>EXIT|exit|QUIT|quit {
46 	return EXIT;
47 }
48 
49 <INITIAL>HELP|help {
50 	return HELP;
51 }
52 
53 <INITIAL>TIMEOUT|timeout {
54 	return TIMEOUT;
55 }
56 
57 <INITIAL>OPEN|open {
58 	BEGIN(WANT_DEVICE);
59 	return OPEN;
60 }
61 
62 <INITIAL>CLOSE|close {
63 	BEGIN(WANT_DEVICE);
64 	return CLOSE;
65 }
66 
67 <INITIAL>READ|read {
68 	BEGIN(WANT_DEVICE);
69 	return READ;
70 }
71 
72 <INITIAL>READBUF|readbuf {
73 	BEGIN(WANT_DEVICE);
74 	return READBUF;
75 }
76 
77 <INITIAL>WRITEBUF|writebuf {
78 	BEGIN(WANT_DEVICE);
79 	return WRITEBUF;
80 }
81 
82 <INITIAL>WRITE|write {
83 	BEGIN(WANT_DEVICE);
84 	return WRITE;
85 }
86 
87 <INITIAL>SETTRIG|settrig {
88 	BEGIN(WANT_DEVICE);
89 	return SETTRIG;
90 }
91 
92 <INITIAL>GETTRIG|gettrig {
93 	BEGIN(WANT_DEVICE);
94 	return GETTRIG;
95 }
96 
97 <INITIAL>SET|set {
98 	BEGIN(WANT_DEVICE);
99 	return SET;
100 }
101 
102 <WANT_DEVICE>{WORD} {
103 	struct parser_pdata *pdata = yyget_extra(yyscanner);
104 	struct iio_device *dev = iio_context_find_device(pdata->ctx, yytext);
105 	yylval->dev = dev;
106 	pdata->dev = dev;
107 	BEGIN(WANT_CHN_OR_ATTR);
108 	return DEVICE;
109 }
110 
111 <WANT_CHN_OR_ATTR>BUFFERS_COUNT|buffers_count {
112 	BEGIN(WANT_VALUE);
113 	return BUFFERS_COUNT;
114 }
115 
116 <WANT_CHN_OR_ATTR>DEBUG|debug {
117 	BEGIN(WANT_ATTR);
118 	return DEBUG_ATTR;
119 }
120 
121 <WANT_CHN_OR_ATTR>BUFFER|buffer {
122 	BEGIN(WANT_ATTR);
123 	return BUFFER_ATTR;
124 }
125 
126 <WANT_CHN_OR_ATTR>INPUT|input|OUTPUT|output {
127 	struct parser_pdata *pdata = yyget_extra(yyscanner);
128 	pdata->channel_is_output = yytext[0] == 'o' || yytext[0] == 'O';
129 	BEGIN(WANT_CHN);
130 	return IN_OUT;
131 }
132 
133 <WANT_CHN>{WORD} {
134 	struct parser_pdata *pdata = yyget_extra(yyscanner);
135 	struct iio_channel *chn = NULL;
136 	if (pdata->dev)
137 		chn = iio_device_find_channel(pdata->dev,
138 					yytext, pdata->channel_is_output);
139 	yylval->chn = chn;
140 	pdata->chn = chn;
141 	BEGIN(WANT_ATTR);
142 	return CHANNEL;
143 }
144 
145 <WANT_VALUE>{WORD} {
146 	char *end;
147 	char errstr[100];
148 
149 	errno = 0;
150 	yylval->value = strtol(yytext, &end, 10);
151 	if (yytext == end || errno == ERANGE) {
152 		sprintf(errstr,"lex : bad long constant: %s",(char*)yytext);
153 		perror(errstr);
154 	}
155 	return VALUE;
156 }
157 
158 CYCLIC|cyclic {
159 	return CYCLIC;
160 }
161 
162 {WORD} {
163 	yylval->word = strdup(yytext);
164 	return WORD;
165 }
166 
167 [ \t]+ {
168 	return SPACE;
169 }
170 
171 [ \t]*\r?\n {
172 	BEGIN(INITIAL);
173 	return END;
174 }
175 
176 . {
177 	BEGIN(INITIAL);
178 }
179