Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
libscpi/src/lexer.c@0:be95bfb06686, 2022-01-17 (annotated)
- Committer:
- wuliqunyy
- Date:
- Mon Jan 17 13:20:09 2022 +0000
- Revision:
- 0:be95bfb06686
a working non_flat + adc_didt for ehp regulation version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wuliqunyy | 0:be95bfb06686 | 1 | /*- |
wuliqunyy | 0:be95bfb06686 | 2 | * BSD 2-Clause License |
wuliqunyy | 0:be95bfb06686 | 3 | * |
wuliqunyy | 0:be95bfb06686 | 4 | * Copyright (c) 2012-2018, Jan Breuer |
wuliqunyy | 0:be95bfb06686 | 5 | * All rights reserved. |
wuliqunyy | 0:be95bfb06686 | 6 | * |
wuliqunyy | 0:be95bfb06686 | 7 | * Redistribution and use in source and binary forms, with or without |
wuliqunyy | 0:be95bfb06686 | 8 | * modification, are permitted provided that the following conditions are met: |
wuliqunyy | 0:be95bfb06686 | 9 | * |
wuliqunyy | 0:be95bfb06686 | 10 | * * Redistributions of source code must retain the above copyright notice, this |
wuliqunyy | 0:be95bfb06686 | 11 | * list of conditions and the following disclaimer. |
wuliqunyy | 0:be95bfb06686 | 12 | * |
wuliqunyy | 0:be95bfb06686 | 13 | * * Redistributions in binary form must reproduce the above copyright notice, |
wuliqunyy | 0:be95bfb06686 | 14 | * this list of conditions and the following disclaimer in the documentation |
wuliqunyy | 0:be95bfb06686 | 15 | * and/or other materials provided with the distribution. |
wuliqunyy | 0:be95bfb06686 | 16 | * |
wuliqunyy | 0:be95bfb06686 | 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
wuliqunyy | 0:be95bfb06686 | 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
wuliqunyy | 0:be95bfb06686 | 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
wuliqunyy | 0:be95bfb06686 | 20 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
wuliqunyy | 0:be95bfb06686 | 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
wuliqunyy | 0:be95bfb06686 | 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
wuliqunyy | 0:be95bfb06686 | 23 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
wuliqunyy | 0:be95bfb06686 | 24 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
wuliqunyy | 0:be95bfb06686 | 25 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
wuliqunyy | 0:be95bfb06686 | 26 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
wuliqunyy | 0:be95bfb06686 | 27 | */ |
wuliqunyy | 0:be95bfb06686 | 28 | |
wuliqunyy | 0:be95bfb06686 | 29 | /** |
wuliqunyy | 0:be95bfb06686 | 30 | * @file lexer.c |
wuliqunyy | 0:be95bfb06686 | 31 | * @date Wed Mar 20 19:35:19 UTC 2013 |
wuliqunyy | 0:be95bfb06686 | 32 | * |
wuliqunyy | 0:be95bfb06686 | 33 | * @brief SCPI Lexer |
wuliqunyy | 0:be95bfb06686 | 34 | * |
wuliqunyy | 0:be95bfb06686 | 35 | * |
wuliqunyy | 0:be95bfb06686 | 36 | */ |
wuliqunyy | 0:be95bfb06686 | 37 | |
wuliqunyy | 0:be95bfb06686 | 38 | #include <ctype.h> |
wuliqunyy | 0:be95bfb06686 | 39 | #include <stdio.h> |
wuliqunyy | 0:be95bfb06686 | 40 | #include <string.h> |
wuliqunyy | 0:be95bfb06686 | 41 | |
wuliqunyy | 0:be95bfb06686 | 42 | #include "lexer_private.h" |
wuliqunyy | 0:be95bfb06686 | 43 | #include "scpi/error.h" |
wuliqunyy | 0:be95bfb06686 | 44 | |
wuliqunyy | 0:be95bfb06686 | 45 | /** |
wuliqunyy | 0:be95bfb06686 | 46 | * Is white space |
wuliqunyy | 0:be95bfb06686 | 47 | * @param c |
wuliqunyy | 0:be95bfb06686 | 48 | * @return |
wuliqunyy | 0:be95bfb06686 | 49 | */ |
wuliqunyy | 0:be95bfb06686 | 50 | static int isws(int c) { |
wuliqunyy | 0:be95bfb06686 | 51 | if ((c == ' ') || (c == '\t')) { |
wuliqunyy | 0:be95bfb06686 | 52 | return 1; |
wuliqunyy | 0:be95bfb06686 | 53 | } |
wuliqunyy | 0:be95bfb06686 | 54 | return 0; |
wuliqunyy | 0:be95bfb06686 | 55 | } |
wuliqunyy | 0:be95bfb06686 | 56 | |
wuliqunyy | 0:be95bfb06686 | 57 | /** |
wuliqunyy | 0:be95bfb06686 | 58 | * Is binary digit |
wuliqunyy | 0:be95bfb06686 | 59 | * @param c |
wuliqunyy | 0:be95bfb06686 | 60 | * @return |
wuliqunyy | 0:be95bfb06686 | 61 | */ |
wuliqunyy | 0:be95bfb06686 | 62 | static int isbdigit(int c) { |
wuliqunyy | 0:be95bfb06686 | 63 | if ((c == '0') || (c == '1')) { |
wuliqunyy | 0:be95bfb06686 | 64 | return 1; |
wuliqunyy | 0:be95bfb06686 | 65 | } |
wuliqunyy | 0:be95bfb06686 | 66 | return 0; |
wuliqunyy | 0:be95bfb06686 | 67 | } |
wuliqunyy | 0:be95bfb06686 | 68 | |
wuliqunyy | 0:be95bfb06686 | 69 | /** |
wuliqunyy | 0:be95bfb06686 | 70 | * Is hexadecimal digit |
wuliqunyy | 0:be95bfb06686 | 71 | * @param c |
wuliqunyy | 0:be95bfb06686 | 72 | * @return |
wuliqunyy | 0:be95bfb06686 | 73 | */ |
wuliqunyy | 0:be95bfb06686 | 74 | static int isqdigit(int c) { |
wuliqunyy | 0:be95bfb06686 | 75 | if ((c == '0') || (c == '1') || (c == '2') || (c == '3') || (c == '4') || (c == '5') || (c == '6') || (c == '7')) { |
wuliqunyy | 0:be95bfb06686 | 76 | return 1; |
wuliqunyy | 0:be95bfb06686 | 77 | } |
wuliqunyy | 0:be95bfb06686 | 78 | return 0; |
wuliqunyy | 0:be95bfb06686 | 79 | } |
wuliqunyy | 0:be95bfb06686 | 80 | |
wuliqunyy | 0:be95bfb06686 | 81 | /** |
wuliqunyy | 0:be95bfb06686 | 82 | * Is end of string |
wuliqunyy | 0:be95bfb06686 | 83 | * @param state |
wuliqunyy | 0:be95bfb06686 | 84 | * @return |
wuliqunyy | 0:be95bfb06686 | 85 | */ |
wuliqunyy | 0:be95bfb06686 | 86 | static int iseos(lex_state_t * state) { |
wuliqunyy | 0:be95bfb06686 | 87 | if ((state->buffer + state->len) <= (state->pos)) { |
wuliqunyy | 0:be95bfb06686 | 88 | return 1; |
wuliqunyy | 0:be95bfb06686 | 89 | } else { |
wuliqunyy | 0:be95bfb06686 | 90 | return 0; |
wuliqunyy | 0:be95bfb06686 | 91 | } |
wuliqunyy | 0:be95bfb06686 | 92 | } |
wuliqunyy | 0:be95bfb06686 | 93 | |
wuliqunyy | 0:be95bfb06686 | 94 | /** |
wuliqunyy | 0:be95bfb06686 | 95 | * Private export of iseos |
wuliqunyy | 0:be95bfb06686 | 96 | * @param state |
wuliqunyy | 0:be95bfb06686 | 97 | * @return |
wuliqunyy | 0:be95bfb06686 | 98 | */ |
wuliqunyy | 0:be95bfb06686 | 99 | int scpiLex_IsEos(lex_state_t * state) { |
wuliqunyy | 0:be95bfb06686 | 100 | return iseos(state); |
wuliqunyy | 0:be95bfb06686 | 101 | } |
wuliqunyy | 0:be95bfb06686 | 102 | |
wuliqunyy | 0:be95bfb06686 | 103 | /** |
wuliqunyy | 0:be95bfb06686 | 104 | * Test current character |
wuliqunyy | 0:be95bfb06686 | 105 | * @param state |
wuliqunyy | 0:be95bfb06686 | 106 | * @param chr |
wuliqunyy | 0:be95bfb06686 | 107 | * @return |
wuliqunyy | 0:be95bfb06686 | 108 | */ |
wuliqunyy | 0:be95bfb06686 | 109 | static int ischr(lex_state_t * state, char chr) { |
wuliqunyy | 0:be95bfb06686 | 110 | return (state->pos[0] == chr); |
wuliqunyy | 0:be95bfb06686 | 111 | } |
wuliqunyy | 0:be95bfb06686 | 112 | |
wuliqunyy | 0:be95bfb06686 | 113 | /** |
wuliqunyy | 0:be95bfb06686 | 114 | * Is plus or minus |
wuliqunyy | 0:be95bfb06686 | 115 | * @param c |
wuliqunyy | 0:be95bfb06686 | 116 | * @return |
wuliqunyy | 0:be95bfb06686 | 117 | */ |
wuliqunyy | 0:be95bfb06686 | 118 | static int isplusmn(int c) { |
wuliqunyy | 0:be95bfb06686 | 119 | return c == '+' || c == '-'; |
wuliqunyy | 0:be95bfb06686 | 120 | } |
wuliqunyy | 0:be95bfb06686 | 121 | |
wuliqunyy | 0:be95bfb06686 | 122 | /** |
wuliqunyy | 0:be95bfb06686 | 123 | * Is letter H |
wuliqunyy | 0:be95bfb06686 | 124 | * @param c |
wuliqunyy | 0:be95bfb06686 | 125 | * @return |
wuliqunyy | 0:be95bfb06686 | 126 | */ |
wuliqunyy | 0:be95bfb06686 | 127 | static int isH(int c) { |
wuliqunyy | 0:be95bfb06686 | 128 | return c == 'h' || c == 'H'; |
wuliqunyy | 0:be95bfb06686 | 129 | } |
wuliqunyy | 0:be95bfb06686 | 130 | |
wuliqunyy | 0:be95bfb06686 | 131 | /** |
wuliqunyy | 0:be95bfb06686 | 132 | * Is letter B |
wuliqunyy | 0:be95bfb06686 | 133 | * @param c |
wuliqunyy | 0:be95bfb06686 | 134 | * @return |
wuliqunyy | 0:be95bfb06686 | 135 | */ |
wuliqunyy | 0:be95bfb06686 | 136 | static int isB(int c) { |
wuliqunyy | 0:be95bfb06686 | 137 | return c == 'b' || c == 'B'; |
wuliqunyy | 0:be95bfb06686 | 138 | } |
wuliqunyy | 0:be95bfb06686 | 139 | |
wuliqunyy | 0:be95bfb06686 | 140 | /** |
wuliqunyy | 0:be95bfb06686 | 141 | * Is letter Q |
wuliqunyy | 0:be95bfb06686 | 142 | * @param c |
wuliqunyy | 0:be95bfb06686 | 143 | * @return |
wuliqunyy | 0:be95bfb06686 | 144 | */ |
wuliqunyy | 0:be95bfb06686 | 145 | static int isQ(int c) { |
wuliqunyy | 0:be95bfb06686 | 146 | return c == 'q' || c == 'Q'; |
wuliqunyy | 0:be95bfb06686 | 147 | } |
wuliqunyy | 0:be95bfb06686 | 148 | |
wuliqunyy | 0:be95bfb06686 | 149 | /** |
wuliqunyy | 0:be95bfb06686 | 150 | * Is letter E |
wuliqunyy | 0:be95bfb06686 | 151 | * @param c |
wuliqunyy | 0:be95bfb06686 | 152 | * @return |
wuliqunyy | 0:be95bfb06686 | 153 | */ |
wuliqunyy | 0:be95bfb06686 | 154 | static int isE(int c) { |
wuliqunyy | 0:be95bfb06686 | 155 | return c == 'e' || c == 'E'; |
wuliqunyy | 0:be95bfb06686 | 156 | } |
wuliqunyy | 0:be95bfb06686 | 157 | |
wuliqunyy | 0:be95bfb06686 | 158 | #define SKIP_NONE 0 |
wuliqunyy | 0:be95bfb06686 | 159 | #define SKIP_OK 1 |
wuliqunyy | 0:be95bfb06686 | 160 | #define SKIP_INCOMPLETE -1 |
wuliqunyy | 0:be95bfb06686 | 161 | |
wuliqunyy | 0:be95bfb06686 | 162 | /* skip characters */ |
wuliqunyy | 0:be95bfb06686 | 163 | /* 7.4.1 <PROGRAM MESSAGE UNIT SEPARATOR>*/ |
wuliqunyy | 0:be95bfb06686 | 164 | /* TODO: static int skipProgramMessageUnitSeparator(lex_state_t * state) */ |
wuliqunyy | 0:be95bfb06686 | 165 | |
wuliqunyy | 0:be95bfb06686 | 166 | /** |
wuliqunyy | 0:be95bfb06686 | 167 | * Skip all whitespaces |
wuliqunyy | 0:be95bfb06686 | 168 | * @param state |
wuliqunyy | 0:be95bfb06686 | 169 | * @return |
wuliqunyy | 0:be95bfb06686 | 170 | */ |
wuliqunyy | 0:be95bfb06686 | 171 | static int skipWs(lex_state_t * state) { |
wuliqunyy | 0:be95bfb06686 | 172 | int someSpace = 0; |
wuliqunyy | 0:be95bfb06686 | 173 | while (!iseos(state) && isws(state->pos[0])) { |
wuliqunyy | 0:be95bfb06686 | 174 | state->pos++; |
wuliqunyy | 0:be95bfb06686 | 175 | someSpace++; |
wuliqunyy | 0:be95bfb06686 | 176 | } |
wuliqunyy | 0:be95bfb06686 | 177 | |
wuliqunyy | 0:be95bfb06686 | 178 | return someSpace; |
wuliqunyy | 0:be95bfb06686 | 179 | } |
wuliqunyy | 0:be95bfb06686 | 180 | |
wuliqunyy | 0:be95bfb06686 | 181 | /* 7.4.2 <PROGRAM DATA SEPARATOR> */ |
wuliqunyy | 0:be95bfb06686 | 182 | /* static int skipProgramDataSeparator(lex_state_t * state) */ |
wuliqunyy | 0:be95bfb06686 | 183 | |
wuliqunyy | 0:be95bfb06686 | 184 | /* 7.5.2 <PROGRAM MESSAGE TERMINATOR> */ |
wuliqunyy | 0:be95bfb06686 | 185 | /* static int skipProgramMessageTerminator(lex_state_t * state) */ |
wuliqunyy | 0:be95bfb06686 | 186 | |
wuliqunyy | 0:be95bfb06686 | 187 | /** |
wuliqunyy | 0:be95bfb06686 | 188 | * Skip decimal digit |
wuliqunyy | 0:be95bfb06686 | 189 | * @param state |
wuliqunyy | 0:be95bfb06686 | 190 | * @return |
wuliqunyy | 0:be95bfb06686 | 191 | */ |
wuliqunyy | 0:be95bfb06686 | 192 | static int skipDigit(lex_state_t * state) { |
wuliqunyy | 0:be95bfb06686 | 193 | if (!iseos(state) && isdigit((uint8_t)(state->pos[0]))) { |
wuliqunyy | 0:be95bfb06686 | 194 | state->pos++; |
wuliqunyy | 0:be95bfb06686 | 195 | return SKIP_OK; |
wuliqunyy | 0:be95bfb06686 | 196 | } else { |
wuliqunyy | 0:be95bfb06686 | 197 | return SKIP_NONE; |
wuliqunyy | 0:be95bfb06686 | 198 | } |
wuliqunyy | 0:be95bfb06686 | 199 | } |
wuliqunyy | 0:be95bfb06686 | 200 | |
wuliqunyy | 0:be95bfb06686 | 201 | /** |
wuliqunyy | 0:be95bfb06686 | 202 | * Skip multiple decimal digits |
wuliqunyy | 0:be95bfb06686 | 203 | * @param state |
wuliqunyy | 0:be95bfb06686 | 204 | * @return |
wuliqunyy | 0:be95bfb06686 | 205 | */ |
wuliqunyy | 0:be95bfb06686 | 206 | static int skipNumbers(lex_state_t * state) { |
wuliqunyy | 0:be95bfb06686 | 207 | int someNumbers = 0; |
wuliqunyy | 0:be95bfb06686 | 208 | while (!iseos(state) && isdigit((uint8_t)(state->pos[0]))) { |
wuliqunyy | 0:be95bfb06686 | 209 | state->pos++; |
wuliqunyy | 0:be95bfb06686 | 210 | someNumbers++; |
wuliqunyy | 0:be95bfb06686 | 211 | } |
wuliqunyy | 0:be95bfb06686 | 212 | return someNumbers; |
wuliqunyy | 0:be95bfb06686 | 213 | } |
wuliqunyy | 0:be95bfb06686 | 214 | |
wuliqunyy | 0:be95bfb06686 | 215 | /** |
wuliqunyy | 0:be95bfb06686 | 216 | * Skip plus or minus |
wuliqunyy | 0:be95bfb06686 | 217 | * @param state |
wuliqunyy | 0:be95bfb06686 | 218 | * @return |
wuliqunyy | 0:be95bfb06686 | 219 | */ |
wuliqunyy | 0:be95bfb06686 | 220 | static int skipPlusmn(lex_state_t * state) { |
wuliqunyy | 0:be95bfb06686 | 221 | if (!iseos(state) && isplusmn(state->pos[0])) { |
wuliqunyy | 0:be95bfb06686 | 222 | state->pos++; |
wuliqunyy | 0:be95bfb06686 | 223 | return SKIP_OK; |
wuliqunyy | 0:be95bfb06686 | 224 | } else { |
wuliqunyy | 0:be95bfb06686 | 225 | return SKIP_NONE; |
wuliqunyy | 0:be95bfb06686 | 226 | } |
wuliqunyy | 0:be95bfb06686 | 227 | } |
wuliqunyy | 0:be95bfb06686 | 228 | |
wuliqunyy | 0:be95bfb06686 | 229 | /** |
wuliqunyy | 0:be95bfb06686 | 230 | * Skip any character from 'a'-'Z' |
wuliqunyy | 0:be95bfb06686 | 231 | * @param state |
wuliqunyy | 0:be95bfb06686 | 232 | * @return |
wuliqunyy | 0:be95bfb06686 | 233 | */ |
wuliqunyy | 0:be95bfb06686 | 234 | static int skipAlpha(lex_state_t * state) { |
wuliqunyy | 0:be95bfb06686 | 235 | int someLetters = 0; |
wuliqunyy | 0:be95bfb06686 | 236 | while (!iseos(state) && isalpha((uint8_t)(state->pos[0]))) { |
wuliqunyy | 0:be95bfb06686 | 237 | state->pos++; |
wuliqunyy | 0:be95bfb06686 | 238 | someLetters++; |
wuliqunyy | 0:be95bfb06686 | 239 | } |
wuliqunyy | 0:be95bfb06686 | 240 | return someLetters; |
wuliqunyy | 0:be95bfb06686 | 241 | } |
wuliqunyy | 0:be95bfb06686 | 242 | |
wuliqunyy | 0:be95bfb06686 | 243 | /** |
wuliqunyy | 0:be95bfb06686 | 244 | * Skip exact character chr or nothing |
wuliqunyy | 0:be95bfb06686 | 245 | * @param state |
wuliqunyy | 0:be95bfb06686 | 246 | * @param chr |
wuliqunyy | 0:be95bfb06686 | 247 | * @return |
wuliqunyy | 0:be95bfb06686 | 248 | */ |
wuliqunyy | 0:be95bfb06686 | 249 | static int skipChr(lex_state_t * state, char chr) { |
wuliqunyy | 0:be95bfb06686 | 250 | if (!iseos(state) && ischr(state, chr)) { |
wuliqunyy | 0:be95bfb06686 | 251 | state->pos++; |
wuliqunyy | 0:be95bfb06686 | 252 | return SKIP_OK; |
wuliqunyy | 0:be95bfb06686 | 253 | } else { |
wuliqunyy | 0:be95bfb06686 | 254 | return SKIP_NONE; |
wuliqunyy | 0:be95bfb06686 | 255 | } |
wuliqunyy | 0:be95bfb06686 | 256 | } |
wuliqunyy | 0:be95bfb06686 | 257 | |
wuliqunyy | 0:be95bfb06686 | 258 | /** |
wuliqunyy | 0:be95bfb06686 | 259 | * Skip slash or dot |
wuliqunyy | 0:be95bfb06686 | 260 | * @param state |
wuliqunyy | 0:be95bfb06686 | 261 | * @return |
wuliqunyy | 0:be95bfb06686 | 262 | */ |
wuliqunyy | 0:be95bfb06686 | 263 | static int skipSlashDot(lex_state_t * state) { |
wuliqunyy | 0:be95bfb06686 | 264 | if (!iseos(state) && (ischr(state, '/') | ischr(state, '.'))) { |
wuliqunyy | 0:be95bfb06686 | 265 | state->pos++; |
wuliqunyy | 0:be95bfb06686 | 266 | return SKIP_OK; |
wuliqunyy | 0:be95bfb06686 | 267 | } else { |
wuliqunyy | 0:be95bfb06686 | 268 | return SKIP_NONE; |
wuliqunyy | 0:be95bfb06686 | 269 | } |
wuliqunyy | 0:be95bfb06686 | 270 | } |
wuliqunyy | 0:be95bfb06686 | 271 | |
wuliqunyy | 0:be95bfb06686 | 272 | /** |
wuliqunyy | 0:be95bfb06686 | 273 | * Skip star |
wuliqunyy | 0:be95bfb06686 | 274 | * @param state |
wuliqunyy | 0:be95bfb06686 | 275 | * @return |
wuliqunyy | 0:be95bfb06686 | 276 | */ |
wuliqunyy | 0:be95bfb06686 | 277 | static int skipStar(lex_state_t * state) { |
wuliqunyy | 0:be95bfb06686 | 278 | if (!iseos(state) && ischr(state, '*')) { |
wuliqunyy | 0:be95bfb06686 | 279 | state->pos++; |
wuliqunyy | 0:be95bfb06686 | 280 | return SKIP_OK; |
wuliqunyy | 0:be95bfb06686 | 281 | } else { |
wuliqunyy | 0:be95bfb06686 | 282 | return SKIP_NONE; |
wuliqunyy | 0:be95bfb06686 | 283 | } |
wuliqunyy | 0:be95bfb06686 | 284 | } |
wuliqunyy | 0:be95bfb06686 | 285 | |
wuliqunyy | 0:be95bfb06686 | 286 | /** |
wuliqunyy | 0:be95bfb06686 | 287 | * Skip colon |
wuliqunyy | 0:be95bfb06686 | 288 | * @param state |
wuliqunyy | 0:be95bfb06686 | 289 | * @return |
wuliqunyy | 0:be95bfb06686 | 290 | */ |
wuliqunyy | 0:be95bfb06686 | 291 | static int skipColon(lex_state_t * state) { |
wuliqunyy | 0:be95bfb06686 | 292 | if (!iseos(state) && ischr(state, ':')) { |
wuliqunyy | 0:be95bfb06686 | 293 | state->pos++; |
wuliqunyy | 0:be95bfb06686 | 294 | return SKIP_OK; |
wuliqunyy | 0:be95bfb06686 | 295 | } else { |
wuliqunyy | 0:be95bfb06686 | 296 | return SKIP_NONE; |
wuliqunyy | 0:be95bfb06686 | 297 | } |
wuliqunyy | 0:be95bfb06686 | 298 | } |
wuliqunyy | 0:be95bfb06686 | 299 | |
wuliqunyy | 0:be95bfb06686 | 300 | /* 7.6.1.2 <COMMAND PROGRAM HEADER> */ |
wuliqunyy | 0:be95bfb06686 | 301 | |
wuliqunyy | 0:be95bfb06686 | 302 | /** |
wuliqunyy | 0:be95bfb06686 | 303 | * Skip program mnemonic [a-z][a-z0-9_]* |
wuliqunyy | 0:be95bfb06686 | 304 | * @param state |
wuliqunyy | 0:be95bfb06686 | 305 | * @return |
wuliqunyy | 0:be95bfb06686 | 306 | */ |
wuliqunyy | 0:be95bfb06686 | 307 | static int skipProgramMnemonic(lex_state_t * state) { |
wuliqunyy | 0:be95bfb06686 | 308 | const char * startPos = state->pos; |
wuliqunyy | 0:be95bfb06686 | 309 | if (!iseos(state) && isalpha((uint8_t)(state->pos[0]))) { |
wuliqunyy | 0:be95bfb06686 | 310 | state->pos++; |
wuliqunyy | 0:be95bfb06686 | 311 | while (!iseos(state) && (isalnum((uint8_t)(state->pos[0])) || ischr(state, '_'))) { |
wuliqunyy | 0:be95bfb06686 | 312 | state->pos++; |
wuliqunyy | 0:be95bfb06686 | 313 | } |
wuliqunyy | 0:be95bfb06686 | 314 | } |
wuliqunyy | 0:be95bfb06686 | 315 | |
wuliqunyy | 0:be95bfb06686 | 316 | if (iseos(state)) { |
wuliqunyy | 0:be95bfb06686 | 317 | return (state->pos - startPos) * SKIP_INCOMPLETE; |
wuliqunyy | 0:be95bfb06686 | 318 | } else { |
wuliqunyy | 0:be95bfb06686 | 319 | return (state->pos - startPos) * SKIP_OK; |
wuliqunyy | 0:be95bfb06686 | 320 | } |
wuliqunyy | 0:be95bfb06686 | 321 | } |
wuliqunyy | 0:be95bfb06686 | 322 | |
wuliqunyy | 0:be95bfb06686 | 323 | /* tokens */ |
wuliqunyy | 0:be95bfb06686 | 324 | |
wuliqunyy | 0:be95bfb06686 | 325 | /** |
wuliqunyy | 0:be95bfb06686 | 326 | * Detect token white space |
wuliqunyy | 0:be95bfb06686 | 327 | * @param state |
wuliqunyy | 0:be95bfb06686 | 328 | * @param token |
wuliqunyy | 0:be95bfb06686 | 329 | * @return |
wuliqunyy | 0:be95bfb06686 | 330 | */ |
wuliqunyy | 0:be95bfb06686 | 331 | int scpiLex_WhiteSpace(lex_state_t * state, scpi_token_t * token) { |
wuliqunyy | 0:be95bfb06686 | 332 | token->ptr = state->pos; |
wuliqunyy | 0:be95bfb06686 | 333 | |
wuliqunyy | 0:be95bfb06686 | 334 | skipWs(state); |
wuliqunyy | 0:be95bfb06686 | 335 | |
wuliqunyy | 0:be95bfb06686 | 336 | token->len = state->pos - token->ptr; |
wuliqunyy | 0:be95bfb06686 | 337 | |
wuliqunyy | 0:be95bfb06686 | 338 | if (token->len > 0) { |
wuliqunyy | 0:be95bfb06686 | 339 | token->type = SCPI_TOKEN_WS; |
wuliqunyy | 0:be95bfb06686 | 340 | } else { |
wuliqunyy | 0:be95bfb06686 | 341 | token->type = SCPI_TOKEN_UNKNOWN; |
wuliqunyy | 0:be95bfb06686 | 342 | } |
wuliqunyy | 0:be95bfb06686 | 343 | |
wuliqunyy | 0:be95bfb06686 | 344 | return token->len; |
wuliqunyy | 0:be95bfb06686 | 345 | } |
wuliqunyy | 0:be95bfb06686 | 346 | |
wuliqunyy | 0:be95bfb06686 | 347 | /* 7.6.1 <COMMAND PROGRAM HEADER> */ |
wuliqunyy | 0:be95bfb06686 | 348 | |
wuliqunyy | 0:be95bfb06686 | 349 | /** |
wuliqunyy | 0:be95bfb06686 | 350 | * Skip command program header \*<PROGRAM MNEMONIC> |
wuliqunyy | 0:be95bfb06686 | 351 | * @param state |
wuliqunyy | 0:be95bfb06686 | 352 | * @return |
wuliqunyy | 0:be95bfb06686 | 353 | */ |
wuliqunyy | 0:be95bfb06686 | 354 | static int skipCommonProgramHeader(lex_state_t * state) { |
wuliqunyy | 0:be95bfb06686 | 355 | int res; |
wuliqunyy | 0:be95bfb06686 | 356 | if (skipStar(state)) { |
wuliqunyy | 0:be95bfb06686 | 357 | res = skipProgramMnemonic(state); |
wuliqunyy | 0:be95bfb06686 | 358 | if (res == SKIP_NONE && iseos(state)) { |
wuliqunyy | 0:be95bfb06686 | 359 | return SKIP_INCOMPLETE; |
wuliqunyy | 0:be95bfb06686 | 360 | } else if (res <= SKIP_INCOMPLETE) { |
wuliqunyy | 0:be95bfb06686 | 361 | return SKIP_OK; |
wuliqunyy | 0:be95bfb06686 | 362 | } else if (res >= SKIP_OK) { |
wuliqunyy | 0:be95bfb06686 | 363 | return SKIP_OK; |
wuliqunyy | 0:be95bfb06686 | 364 | } else { |
wuliqunyy | 0:be95bfb06686 | 365 | return SKIP_INCOMPLETE; |
wuliqunyy | 0:be95bfb06686 | 366 | } |
wuliqunyy | 0:be95bfb06686 | 367 | } |
wuliqunyy | 0:be95bfb06686 | 368 | return SKIP_NONE; |
wuliqunyy | 0:be95bfb06686 | 369 | } |
wuliqunyy | 0:be95bfb06686 | 370 | |
wuliqunyy | 0:be95bfb06686 | 371 | /** |
wuliqunyy | 0:be95bfb06686 | 372 | * Skip compound program header :<PROGRAM MNEMONIC>:<PROGRAM MNEMONIC>... |
wuliqunyy | 0:be95bfb06686 | 373 | * @param state |
wuliqunyy | 0:be95bfb06686 | 374 | * @return |
wuliqunyy | 0:be95bfb06686 | 375 | */ |
wuliqunyy | 0:be95bfb06686 | 376 | static int skipCompoundProgramHeader(lex_state_t * state) { |
wuliqunyy | 0:be95bfb06686 | 377 | int res; |
wuliqunyy | 0:be95bfb06686 | 378 | int firstColon = skipColon(state); |
wuliqunyy | 0:be95bfb06686 | 379 | |
wuliqunyy | 0:be95bfb06686 | 380 | res = skipProgramMnemonic(state); |
wuliqunyy | 0:be95bfb06686 | 381 | if (res >= SKIP_OK) { |
wuliqunyy | 0:be95bfb06686 | 382 | while (skipColon(state)) { |
wuliqunyy | 0:be95bfb06686 | 383 | res = skipProgramMnemonic(state); |
wuliqunyy | 0:be95bfb06686 | 384 | if (res <= SKIP_INCOMPLETE) { |
wuliqunyy | 0:be95bfb06686 | 385 | return SKIP_OK; |
wuliqunyy | 0:be95bfb06686 | 386 | } else if (res == SKIP_NONE) { |
wuliqunyy | 0:be95bfb06686 | 387 | return SKIP_INCOMPLETE; |
wuliqunyy | 0:be95bfb06686 | 388 | } |
wuliqunyy | 0:be95bfb06686 | 389 | } |
wuliqunyy | 0:be95bfb06686 | 390 | return SKIP_OK; |
wuliqunyy | 0:be95bfb06686 | 391 | } else if (res <= SKIP_INCOMPLETE) { |
wuliqunyy | 0:be95bfb06686 | 392 | return SKIP_OK; |
wuliqunyy | 0:be95bfb06686 | 393 | } else if (firstColon) { |
wuliqunyy | 0:be95bfb06686 | 394 | return SKIP_INCOMPLETE; |
wuliqunyy | 0:be95bfb06686 | 395 | } else { |
wuliqunyy | 0:be95bfb06686 | 396 | return SKIP_NONE; |
wuliqunyy | 0:be95bfb06686 | 397 | } |
wuliqunyy | 0:be95bfb06686 | 398 | } |
wuliqunyy | 0:be95bfb06686 | 399 | |
wuliqunyy | 0:be95bfb06686 | 400 | /** |
wuliqunyy | 0:be95bfb06686 | 401 | * Detect token command or compound program header |
wuliqunyy | 0:be95bfb06686 | 402 | * @param state |
wuliqunyy | 0:be95bfb06686 | 403 | * @param token |
wuliqunyy | 0:be95bfb06686 | 404 | * @return |
wuliqunyy | 0:be95bfb06686 | 405 | */ |
wuliqunyy | 0:be95bfb06686 | 406 | int scpiLex_ProgramHeader(lex_state_t * state, scpi_token_t * token) { |
wuliqunyy | 0:be95bfb06686 | 407 | int res; |
wuliqunyy | 0:be95bfb06686 | 408 | token->ptr = state->pos; |
wuliqunyy | 0:be95bfb06686 | 409 | token->type = SCPI_TOKEN_UNKNOWN; |
wuliqunyy | 0:be95bfb06686 | 410 | |
wuliqunyy | 0:be95bfb06686 | 411 | res = skipCommonProgramHeader(state); |
wuliqunyy | 0:be95bfb06686 | 412 | if (res >= SKIP_OK) { |
wuliqunyy | 0:be95bfb06686 | 413 | if (skipChr(state, '?') >= SKIP_OK) { |
wuliqunyy | 0:be95bfb06686 | 414 | token->type = SCPI_TOKEN_COMMON_QUERY_PROGRAM_HEADER; |
wuliqunyy | 0:be95bfb06686 | 415 | } else { |
wuliqunyy | 0:be95bfb06686 | 416 | token->type = SCPI_TOKEN_COMMON_PROGRAM_HEADER; |
wuliqunyy | 0:be95bfb06686 | 417 | } |
wuliqunyy | 0:be95bfb06686 | 418 | } else if (res <= SKIP_INCOMPLETE) { |
wuliqunyy | 0:be95bfb06686 | 419 | token->type = SCPI_TOKEN_INCOMPLETE_COMMON_PROGRAM_HEADER; |
wuliqunyy | 0:be95bfb06686 | 420 | } else if (res == SKIP_NONE) { |
wuliqunyy | 0:be95bfb06686 | 421 | res = skipCompoundProgramHeader(state); |
wuliqunyy | 0:be95bfb06686 | 422 | |
wuliqunyy | 0:be95bfb06686 | 423 | if (res >= SKIP_OK) { |
wuliqunyy | 0:be95bfb06686 | 424 | if (skipChr(state, '?') >= SKIP_OK) { |
wuliqunyy | 0:be95bfb06686 | 425 | token->type = SCPI_TOKEN_COMPOUND_QUERY_PROGRAM_HEADER; |
wuliqunyy | 0:be95bfb06686 | 426 | } else { |
wuliqunyy | 0:be95bfb06686 | 427 | token->type = SCPI_TOKEN_COMPOUND_PROGRAM_HEADER; |
wuliqunyy | 0:be95bfb06686 | 428 | } |
wuliqunyy | 0:be95bfb06686 | 429 | } else if (res <= SKIP_INCOMPLETE) { |
wuliqunyy | 0:be95bfb06686 | 430 | token->type = SCPI_TOKEN_INCOMPLETE_COMPOUND_PROGRAM_HEADER; |
wuliqunyy | 0:be95bfb06686 | 431 | } |
wuliqunyy | 0:be95bfb06686 | 432 | } |
wuliqunyy | 0:be95bfb06686 | 433 | |
wuliqunyy | 0:be95bfb06686 | 434 | if (token->type != SCPI_TOKEN_UNKNOWN) { |
wuliqunyy | 0:be95bfb06686 | 435 | token->len = state->pos - token->ptr; |
wuliqunyy | 0:be95bfb06686 | 436 | } else { |
wuliqunyy | 0:be95bfb06686 | 437 | token->len = 0; |
wuliqunyy | 0:be95bfb06686 | 438 | state->pos = token->ptr; |
wuliqunyy | 0:be95bfb06686 | 439 | } |
wuliqunyy | 0:be95bfb06686 | 440 | |
wuliqunyy | 0:be95bfb06686 | 441 | return token->len; |
wuliqunyy | 0:be95bfb06686 | 442 | } |
wuliqunyy | 0:be95bfb06686 | 443 | |
wuliqunyy | 0:be95bfb06686 | 444 | /* 7.7.1 <CHARACTER PROGRAM DATA> */ |
wuliqunyy | 0:be95bfb06686 | 445 | |
wuliqunyy | 0:be95bfb06686 | 446 | /** |
wuliqunyy | 0:be95bfb06686 | 447 | * Detect token "Character program data" |
wuliqunyy | 0:be95bfb06686 | 448 | * @param state |
wuliqunyy | 0:be95bfb06686 | 449 | * @param token |
wuliqunyy | 0:be95bfb06686 | 450 | * @return |
wuliqunyy | 0:be95bfb06686 | 451 | */ |
wuliqunyy | 0:be95bfb06686 | 452 | int scpiLex_CharacterProgramData(lex_state_t * state, scpi_token_t * token) { |
wuliqunyy | 0:be95bfb06686 | 453 | token->ptr = state->pos; |
wuliqunyy | 0:be95bfb06686 | 454 | |
wuliqunyy | 0:be95bfb06686 | 455 | if (!iseos(state) && isalpha((uint8_t)(state->pos[0]))) { |
wuliqunyy | 0:be95bfb06686 | 456 | state->pos++; |
wuliqunyy | 0:be95bfb06686 | 457 | while (!iseos(state) && (isalnum((uint8_t)(state->pos[0])) || ischr(state, '_'))) { |
wuliqunyy | 0:be95bfb06686 | 458 | state->pos++; |
wuliqunyy | 0:be95bfb06686 | 459 | } |
wuliqunyy | 0:be95bfb06686 | 460 | } |
wuliqunyy | 0:be95bfb06686 | 461 | |
wuliqunyy | 0:be95bfb06686 | 462 | token->len = state->pos - token->ptr; |
wuliqunyy | 0:be95bfb06686 | 463 | if (token->len > 0) { |
wuliqunyy | 0:be95bfb06686 | 464 | token->type = SCPI_TOKEN_PROGRAM_MNEMONIC; |
wuliqunyy | 0:be95bfb06686 | 465 | } else { |
wuliqunyy | 0:be95bfb06686 | 466 | token->type = SCPI_TOKEN_UNKNOWN; |
wuliqunyy | 0:be95bfb06686 | 467 | } |
wuliqunyy | 0:be95bfb06686 | 468 | |
wuliqunyy | 0:be95bfb06686 | 469 | return token->len; |
wuliqunyy | 0:be95bfb06686 | 470 | } |
wuliqunyy | 0:be95bfb06686 | 471 | |
wuliqunyy | 0:be95bfb06686 | 472 | /* 7.7.2 <DECIMAL NUMERIC PROGRAM DATA> */ |
wuliqunyy | 0:be95bfb06686 | 473 | static int skipMantisa(lex_state_t * state) { |
wuliqunyy | 0:be95bfb06686 | 474 | int someNumbers = 0; |
wuliqunyy | 0:be95bfb06686 | 475 | |
wuliqunyy | 0:be95bfb06686 | 476 | skipPlusmn(state); |
wuliqunyy | 0:be95bfb06686 | 477 | |
wuliqunyy | 0:be95bfb06686 | 478 | someNumbers += skipNumbers(state); |
wuliqunyy | 0:be95bfb06686 | 479 | |
wuliqunyy | 0:be95bfb06686 | 480 | if (skipChr(state, '.')) { |
wuliqunyy | 0:be95bfb06686 | 481 | someNumbers += skipNumbers(state); |
wuliqunyy | 0:be95bfb06686 | 482 | } |
wuliqunyy | 0:be95bfb06686 | 483 | |
wuliqunyy | 0:be95bfb06686 | 484 | return someNumbers; |
wuliqunyy | 0:be95bfb06686 | 485 | } |
wuliqunyy | 0:be95bfb06686 | 486 | |
wuliqunyy | 0:be95bfb06686 | 487 | static int skipExponent(lex_state_t * state) { |
wuliqunyy | 0:be95bfb06686 | 488 | int someNumbers = 0; |
wuliqunyy | 0:be95bfb06686 | 489 | |
wuliqunyy | 0:be95bfb06686 | 490 | if (!iseos(state) && isE(state->pos[0])) { |
wuliqunyy | 0:be95bfb06686 | 491 | state->pos++; |
wuliqunyy | 0:be95bfb06686 | 492 | |
wuliqunyy | 0:be95bfb06686 | 493 | skipWs(state); |
wuliqunyy | 0:be95bfb06686 | 494 | |
wuliqunyy | 0:be95bfb06686 | 495 | skipPlusmn(state); |
wuliqunyy | 0:be95bfb06686 | 496 | |
wuliqunyy | 0:be95bfb06686 | 497 | someNumbers = skipNumbers(state); |
wuliqunyy | 0:be95bfb06686 | 498 | } |
wuliqunyy | 0:be95bfb06686 | 499 | |
wuliqunyy | 0:be95bfb06686 | 500 | return someNumbers; |
wuliqunyy | 0:be95bfb06686 | 501 | } |
wuliqunyy | 0:be95bfb06686 | 502 | |
wuliqunyy | 0:be95bfb06686 | 503 | /** |
wuliqunyy | 0:be95bfb06686 | 504 | * Detect token Decimal number |
wuliqunyy | 0:be95bfb06686 | 505 | * @param state |
wuliqunyy | 0:be95bfb06686 | 506 | * @param token |
wuliqunyy | 0:be95bfb06686 | 507 | * @return |
wuliqunyy | 0:be95bfb06686 | 508 | */ |
wuliqunyy | 0:be95bfb06686 | 509 | int scpiLex_DecimalNumericProgramData(lex_state_t * state, scpi_token_t * token) { |
wuliqunyy | 0:be95bfb06686 | 510 | char * rollback; |
wuliqunyy | 0:be95bfb06686 | 511 | token->ptr = state->pos; |
wuliqunyy | 0:be95bfb06686 | 512 | |
wuliqunyy | 0:be95bfb06686 | 513 | if (skipMantisa(state)) { |
wuliqunyy | 0:be95bfb06686 | 514 | rollback = state->pos; |
wuliqunyy | 0:be95bfb06686 | 515 | skipWs(state); |
wuliqunyy | 0:be95bfb06686 | 516 | if (!skipExponent(state)) { |
wuliqunyy | 0:be95bfb06686 | 517 | state->pos = rollback; |
wuliqunyy | 0:be95bfb06686 | 518 | } |
wuliqunyy | 0:be95bfb06686 | 519 | } else { |
wuliqunyy | 0:be95bfb06686 | 520 | state->pos = token->ptr; |
wuliqunyy | 0:be95bfb06686 | 521 | } |
wuliqunyy | 0:be95bfb06686 | 522 | |
wuliqunyy | 0:be95bfb06686 | 523 | token->len = state->pos - token->ptr; |
wuliqunyy | 0:be95bfb06686 | 524 | if (token->len > 0) { |
wuliqunyy | 0:be95bfb06686 | 525 | token->type = SCPI_TOKEN_DECIMAL_NUMERIC_PROGRAM_DATA; |
wuliqunyy | 0:be95bfb06686 | 526 | } else { |
wuliqunyy | 0:be95bfb06686 | 527 | token->type = SCPI_TOKEN_UNKNOWN; |
wuliqunyy | 0:be95bfb06686 | 528 | } |
wuliqunyy | 0:be95bfb06686 | 529 | |
wuliqunyy | 0:be95bfb06686 | 530 | return token->len; |
wuliqunyy | 0:be95bfb06686 | 531 | } |
wuliqunyy | 0:be95bfb06686 | 532 | |
wuliqunyy | 0:be95bfb06686 | 533 | /* 7.7.3 <SUFFIX PROGRAM DATA> */ |
wuliqunyy | 0:be95bfb06686 | 534 | int scpiLex_SuffixProgramData(lex_state_t * state, scpi_token_t * token) { |
wuliqunyy | 0:be95bfb06686 | 535 | token->ptr = state->pos; |
wuliqunyy | 0:be95bfb06686 | 536 | |
wuliqunyy | 0:be95bfb06686 | 537 | skipChr(state, '/'); |
wuliqunyy | 0:be95bfb06686 | 538 | |
wuliqunyy | 0:be95bfb06686 | 539 | /* TODO: strict parsing : SLASH? (ALPHA+ (MINUS? DIGIT)?) ((SLASH | DOT) (ALPHA+ (MINUS? DIGIT)?))* */ |
wuliqunyy | 0:be95bfb06686 | 540 | if (skipAlpha(state)) { |
wuliqunyy | 0:be95bfb06686 | 541 | skipChr(state, '-'); |
wuliqunyy | 0:be95bfb06686 | 542 | skipDigit(state); |
wuliqunyy | 0:be95bfb06686 | 543 | |
wuliqunyy | 0:be95bfb06686 | 544 | while (skipSlashDot(state)) { |
wuliqunyy | 0:be95bfb06686 | 545 | skipAlpha(state); |
wuliqunyy | 0:be95bfb06686 | 546 | skipChr(state, '-'); |
wuliqunyy | 0:be95bfb06686 | 547 | skipDigit(state); |
wuliqunyy | 0:be95bfb06686 | 548 | } |
wuliqunyy | 0:be95bfb06686 | 549 | } |
wuliqunyy | 0:be95bfb06686 | 550 | |
wuliqunyy | 0:be95bfb06686 | 551 | token->len = state->pos - token->ptr; |
wuliqunyy | 0:be95bfb06686 | 552 | if ((token->len > 0)) { |
wuliqunyy | 0:be95bfb06686 | 553 | token->type = SCPI_TOKEN_SUFFIX_PROGRAM_DATA; |
wuliqunyy | 0:be95bfb06686 | 554 | } else { |
wuliqunyy | 0:be95bfb06686 | 555 | token->type = SCPI_TOKEN_UNKNOWN; |
wuliqunyy | 0:be95bfb06686 | 556 | state->pos = token->ptr; |
wuliqunyy | 0:be95bfb06686 | 557 | token->len = 0; |
wuliqunyy | 0:be95bfb06686 | 558 | } |
wuliqunyy | 0:be95bfb06686 | 559 | |
wuliqunyy | 0:be95bfb06686 | 560 | return token->len; |
wuliqunyy | 0:be95bfb06686 | 561 | } |
wuliqunyy | 0:be95bfb06686 | 562 | |
wuliqunyy | 0:be95bfb06686 | 563 | /* 7.7.4 <NONDECIMAL NUMERIC PROGRAM DATA> */ |
wuliqunyy | 0:be95bfb06686 | 564 | static int skipHexNum(lex_state_t * state) { |
wuliqunyy | 0:be95bfb06686 | 565 | int someNumbers = 0; |
wuliqunyy | 0:be95bfb06686 | 566 | while (!iseos(state) && isxdigit((uint8_t)(state->pos[0]))) { |
wuliqunyy | 0:be95bfb06686 | 567 | state->pos++; |
wuliqunyy | 0:be95bfb06686 | 568 | someNumbers++; |
wuliqunyy | 0:be95bfb06686 | 569 | } |
wuliqunyy | 0:be95bfb06686 | 570 | return someNumbers; |
wuliqunyy | 0:be95bfb06686 | 571 | } |
wuliqunyy | 0:be95bfb06686 | 572 | |
wuliqunyy | 0:be95bfb06686 | 573 | static int skipOctNum(lex_state_t * state) { |
wuliqunyy | 0:be95bfb06686 | 574 | int someNumbers = 0; |
wuliqunyy | 0:be95bfb06686 | 575 | while (!iseos(state) && isqdigit(state->pos[0])) { |
wuliqunyy | 0:be95bfb06686 | 576 | state->pos++; |
wuliqunyy | 0:be95bfb06686 | 577 | someNumbers++; |
wuliqunyy | 0:be95bfb06686 | 578 | } |
wuliqunyy | 0:be95bfb06686 | 579 | return someNumbers; |
wuliqunyy | 0:be95bfb06686 | 580 | } |
wuliqunyy | 0:be95bfb06686 | 581 | |
wuliqunyy | 0:be95bfb06686 | 582 | static int skipBinNum(lex_state_t * state) { |
wuliqunyy | 0:be95bfb06686 | 583 | int someNumbers = 0; |
wuliqunyy | 0:be95bfb06686 | 584 | while (!iseos(state) && isbdigit(state->pos[0])) { |
wuliqunyy | 0:be95bfb06686 | 585 | state->pos++; |
wuliqunyy | 0:be95bfb06686 | 586 | someNumbers++; |
wuliqunyy | 0:be95bfb06686 | 587 | } |
wuliqunyy | 0:be95bfb06686 | 588 | return someNumbers; |
wuliqunyy | 0:be95bfb06686 | 589 | } |
wuliqunyy | 0:be95bfb06686 | 590 | |
wuliqunyy | 0:be95bfb06686 | 591 | /** |
wuliqunyy | 0:be95bfb06686 | 592 | * Detect token nondecimal number |
wuliqunyy | 0:be95bfb06686 | 593 | * @param state |
wuliqunyy | 0:be95bfb06686 | 594 | * @param token |
wuliqunyy | 0:be95bfb06686 | 595 | * @return |
wuliqunyy | 0:be95bfb06686 | 596 | */ |
wuliqunyy | 0:be95bfb06686 | 597 | int scpiLex_NondecimalNumericData(lex_state_t * state, scpi_token_t * token) { |
wuliqunyy | 0:be95bfb06686 | 598 | int someNumbers = 0; |
wuliqunyy | 0:be95bfb06686 | 599 | token->ptr = state->pos; |
wuliqunyy | 0:be95bfb06686 | 600 | if (skipChr(state, '#')) { |
wuliqunyy | 0:be95bfb06686 | 601 | if (!iseos(state)) { |
wuliqunyy | 0:be95bfb06686 | 602 | if (isH(state->pos[0])) { |
wuliqunyy | 0:be95bfb06686 | 603 | state->pos++; |
wuliqunyy | 0:be95bfb06686 | 604 | someNumbers = skipHexNum(state); |
wuliqunyy | 0:be95bfb06686 | 605 | token->type = SCPI_TOKEN_HEXNUM; |
wuliqunyy | 0:be95bfb06686 | 606 | } else if (isQ(state->pos[0])) { |
wuliqunyy | 0:be95bfb06686 | 607 | state->pos++; |
wuliqunyy | 0:be95bfb06686 | 608 | someNumbers = skipOctNum(state); |
wuliqunyy | 0:be95bfb06686 | 609 | token->type = SCPI_TOKEN_OCTNUM; |
wuliqunyy | 0:be95bfb06686 | 610 | } else if (isB(state->pos[0])) { |
wuliqunyy | 0:be95bfb06686 | 611 | state->pos++; |
wuliqunyy | 0:be95bfb06686 | 612 | someNumbers = skipBinNum(state); |
wuliqunyy | 0:be95bfb06686 | 613 | token->type = SCPI_TOKEN_BINNUM; |
wuliqunyy | 0:be95bfb06686 | 614 | } |
wuliqunyy | 0:be95bfb06686 | 615 | } |
wuliqunyy | 0:be95bfb06686 | 616 | } |
wuliqunyy | 0:be95bfb06686 | 617 | |
wuliqunyy | 0:be95bfb06686 | 618 | if (someNumbers) { |
wuliqunyy | 0:be95bfb06686 | 619 | token->ptr += 2; /* ignore number prefix */ |
wuliqunyy | 0:be95bfb06686 | 620 | token->len = state->pos - token->ptr; |
wuliqunyy | 0:be95bfb06686 | 621 | } else { |
wuliqunyy | 0:be95bfb06686 | 622 | token->type = SCPI_TOKEN_UNKNOWN; |
wuliqunyy | 0:be95bfb06686 | 623 | state->pos = token->ptr; |
wuliqunyy | 0:be95bfb06686 | 624 | token->len = 0; |
wuliqunyy | 0:be95bfb06686 | 625 | } |
wuliqunyy | 0:be95bfb06686 | 626 | return token->len > 0 ? token->len + 2 : 0; |
wuliqunyy | 0:be95bfb06686 | 627 | } |
wuliqunyy | 0:be95bfb06686 | 628 | |
wuliqunyy | 0:be95bfb06686 | 629 | /* 7.7.5 <STRING PROGRAM DATA> */ |
wuliqunyy | 0:be95bfb06686 | 630 | static int isascii7bit(int c) { |
wuliqunyy | 0:be95bfb06686 | 631 | return (c >= 0) && (c <= 0x7f); |
wuliqunyy | 0:be95bfb06686 | 632 | } |
wuliqunyy | 0:be95bfb06686 | 633 | |
wuliqunyy | 0:be95bfb06686 | 634 | static void skipQuoteProgramData(lex_state_t * state, char quote) { |
wuliqunyy | 0:be95bfb06686 | 635 | while (!iseos(state)) { |
wuliqunyy | 0:be95bfb06686 | 636 | if (isascii7bit(state->pos[0]) && !ischr(state, quote)) { |
wuliqunyy | 0:be95bfb06686 | 637 | state->pos++; |
wuliqunyy | 0:be95bfb06686 | 638 | } else if (ischr(state, quote)) { |
wuliqunyy | 0:be95bfb06686 | 639 | state->pos++; |
wuliqunyy | 0:be95bfb06686 | 640 | if (!iseos(state) && ischr(state, quote)) { |
wuliqunyy | 0:be95bfb06686 | 641 | state->pos++; |
wuliqunyy | 0:be95bfb06686 | 642 | } else { |
wuliqunyy | 0:be95bfb06686 | 643 | state->pos--; |
wuliqunyy | 0:be95bfb06686 | 644 | break; |
wuliqunyy | 0:be95bfb06686 | 645 | } |
wuliqunyy | 0:be95bfb06686 | 646 | } else { |
wuliqunyy | 0:be95bfb06686 | 647 | break; |
wuliqunyy | 0:be95bfb06686 | 648 | } |
wuliqunyy | 0:be95bfb06686 | 649 | } |
wuliqunyy | 0:be95bfb06686 | 650 | } |
wuliqunyy | 0:be95bfb06686 | 651 | |
wuliqunyy | 0:be95bfb06686 | 652 | static void skipDoubleQuoteProgramData(lex_state_t * state) { |
wuliqunyy | 0:be95bfb06686 | 653 | skipQuoteProgramData(state, '"'); |
wuliqunyy | 0:be95bfb06686 | 654 | } |
wuliqunyy | 0:be95bfb06686 | 655 | |
wuliqunyy | 0:be95bfb06686 | 656 | static void skipSingleQuoteProgramData(lex_state_t * state) { |
wuliqunyy | 0:be95bfb06686 | 657 | skipQuoteProgramData(state, '\''); |
wuliqunyy | 0:be95bfb06686 | 658 | } |
wuliqunyy | 0:be95bfb06686 | 659 | |
wuliqunyy | 0:be95bfb06686 | 660 | /** |
wuliqunyy | 0:be95bfb06686 | 661 | * Detect token String data |
wuliqunyy | 0:be95bfb06686 | 662 | * @param state |
wuliqunyy | 0:be95bfb06686 | 663 | * @param token |
wuliqunyy | 0:be95bfb06686 | 664 | * @return |
wuliqunyy | 0:be95bfb06686 | 665 | */ |
wuliqunyy | 0:be95bfb06686 | 666 | int scpiLex_StringProgramData(lex_state_t * state, scpi_token_t * token) { |
wuliqunyy | 0:be95bfb06686 | 667 | token->ptr = state->pos; |
wuliqunyy | 0:be95bfb06686 | 668 | |
wuliqunyy | 0:be95bfb06686 | 669 | if (!iseos(state)) { |
wuliqunyy | 0:be95bfb06686 | 670 | if (ischr(state, '"')) { |
wuliqunyy | 0:be95bfb06686 | 671 | state->pos++; |
wuliqunyy | 0:be95bfb06686 | 672 | token->type = SCPI_TOKEN_DOUBLE_QUOTE_PROGRAM_DATA; |
wuliqunyy | 0:be95bfb06686 | 673 | skipDoubleQuoteProgramData(state); |
wuliqunyy | 0:be95bfb06686 | 674 | if (!iseos(state) && ischr(state, '"')) { |
wuliqunyy | 0:be95bfb06686 | 675 | state->pos++; |
wuliqunyy | 0:be95bfb06686 | 676 | token->len = state->pos - token->ptr; |
wuliqunyy | 0:be95bfb06686 | 677 | } else { |
wuliqunyy | 0:be95bfb06686 | 678 | state->pos = token->ptr; |
wuliqunyy | 0:be95bfb06686 | 679 | } |
wuliqunyy | 0:be95bfb06686 | 680 | } else if (ischr(state, '\'')) { |
wuliqunyy | 0:be95bfb06686 | 681 | state->pos++; |
wuliqunyy | 0:be95bfb06686 | 682 | token->type = SCPI_TOKEN_SINGLE_QUOTE_PROGRAM_DATA; |
wuliqunyy | 0:be95bfb06686 | 683 | skipSingleQuoteProgramData(state); |
wuliqunyy | 0:be95bfb06686 | 684 | if (!iseos(state) && ischr(state, '\'')) { |
wuliqunyy | 0:be95bfb06686 | 685 | state->pos++; |
wuliqunyy | 0:be95bfb06686 | 686 | token->len = state->pos - token->ptr; |
wuliqunyy | 0:be95bfb06686 | 687 | } else { |
wuliqunyy | 0:be95bfb06686 | 688 | state->pos = token->ptr; |
wuliqunyy | 0:be95bfb06686 | 689 | } |
wuliqunyy | 0:be95bfb06686 | 690 | } |
wuliqunyy | 0:be95bfb06686 | 691 | } |
wuliqunyy | 0:be95bfb06686 | 692 | |
wuliqunyy | 0:be95bfb06686 | 693 | token->len = state->pos - token->ptr; |
wuliqunyy | 0:be95bfb06686 | 694 | |
wuliqunyy | 0:be95bfb06686 | 695 | if ((token->len > 0)) { |
wuliqunyy | 0:be95bfb06686 | 696 | /* token->ptr++; |
wuliqunyy | 0:be95bfb06686 | 697 | * token->len -= 2; */ |
wuliqunyy | 0:be95bfb06686 | 698 | } else { |
wuliqunyy | 0:be95bfb06686 | 699 | token->type = SCPI_TOKEN_UNKNOWN; |
wuliqunyy | 0:be95bfb06686 | 700 | state->pos = token->ptr; |
wuliqunyy | 0:be95bfb06686 | 701 | token->len = 0; |
wuliqunyy | 0:be95bfb06686 | 702 | } |
wuliqunyy | 0:be95bfb06686 | 703 | |
wuliqunyy | 0:be95bfb06686 | 704 | return token->len > 0 ? token->len : 0; |
wuliqunyy | 0:be95bfb06686 | 705 | } |
wuliqunyy | 0:be95bfb06686 | 706 | |
wuliqunyy | 0:be95bfb06686 | 707 | /* 7.7.6 <ARBITRARY BLOCK PROGRAM DATA> */ |
wuliqunyy | 0:be95bfb06686 | 708 | static int isNonzeroDigit(int c) { |
wuliqunyy | 0:be95bfb06686 | 709 | return isdigit(c) && (c != '0'); |
wuliqunyy | 0:be95bfb06686 | 710 | } |
wuliqunyy | 0:be95bfb06686 | 711 | |
wuliqunyy | 0:be95bfb06686 | 712 | /** |
wuliqunyy | 0:be95bfb06686 | 713 | * Detect token Block Data |
wuliqunyy | 0:be95bfb06686 | 714 | * @param state |
wuliqunyy | 0:be95bfb06686 | 715 | * @param token |
wuliqunyy | 0:be95bfb06686 | 716 | * @return |
wuliqunyy | 0:be95bfb06686 | 717 | */ |
wuliqunyy | 0:be95bfb06686 | 718 | int scpiLex_ArbitraryBlockProgramData(lex_state_t * state, scpi_token_t * token) { |
wuliqunyy | 0:be95bfb06686 | 719 | int i; |
wuliqunyy | 0:be95bfb06686 | 720 | int arbitraryBlockLength = 0; |
wuliqunyy | 0:be95bfb06686 | 721 | const char * ptr = state->pos; |
wuliqunyy | 0:be95bfb06686 | 722 | int validData = -1; |
wuliqunyy | 0:be95bfb06686 | 723 | token->ptr = state->pos; |
wuliqunyy | 0:be95bfb06686 | 724 | |
wuliqunyy | 0:be95bfb06686 | 725 | if (skipChr(state, '#')) { |
wuliqunyy | 0:be95bfb06686 | 726 | if (!iseos(state) && isNonzeroDigit(state->pos[0])) { |
wuliqunyy | 0:be95bfb06686 | 727 | /* Get number of digits */ |
wuliqunyy | 0:be95bfb06686 | 728 | i = state->pos[0] - '0'; |
wuliqunyy | 0:be95bfb06686 | 729 | state->pos++; |
wuliqunyy | 0:be95bfb06686 | 730 | |
wuliqunyy | 0:be95bfb06686 | 731 | for (; i > 0; i--) { |
wuliqunyy | 0:be95bfb06686 | 732 | if (!iseos(state) && isdigit((uint8_t)(state->pos[0]))) { |
wuliqunyy | 0:be95bfb06686 | 733 | arbitraryBlockLength *= 10; |
wuliqunyy | 0:be95bfb06686 | 734 | arbitraryBlockLength += (state->pos[0] - '0'); |
wuliqunyy | 0:be95bfb06686 | 735 | state->pos++; |
wuliqunyy | 0:be95bfb06686 | 736 | } else { |
wuliqunyy | 0:be95bfb06686 | 737 | break; |
wuliqunyy | 0:be95bfb06686 | 738 | } |
wuliqunyy | 0:be95bfb06686 | 739 | } |
wuliqunyy | 0:be95bfb06686 | 740 | |
wuliqunyy | 0:be95bfb06686 | 741 | if (i == 0) { |
wuliqunyy | 0:be95bfb06686 | 742 | state->pos += arbitraryBlockLength; |
wuliqunyy | 0:be95bfb06686 | 743 | if ((state->buffer + state->len) >= (state->pos)) { |
wuliqunyy | 0:be95bfb06686 | 744 | token->ptr = state->pos - arbitraryBlockLength; |
wuliqunyy | 0:be95bfb06686 | 745 | token->len = arbitraryBlockLength; |
wuliqunyy | 0:be95bfb06686 | 746 | validData = 1; |
wuliqunyy | 0:be95bfb06686 | 747 | } else { |
wuliqunyy | 0:be95bfb06686 | 748 | validData = 0; |
wuliqunyy | 0:be95bfb06686 | 749 | } |
wuliqunyy | 0:be95bfb06686 | 750 | } else if (iseos(state)) { |
wuliqunyy | 0:be95bfb06686 | 751 | validData = 0; |
wuliqunyy | 0:be95bfb06686 | 752 | } |
wuliqunyy | 0:be95bfb06686 | 753 | } else if (iseos(state)) { |
wuliqunyy | 0:be95bfb06686 | 754 | validData = 0; |
wuliqunyy | 0:be95bfb06686 | 755 | } |
wuliqunyy | 0:be95bfb06686 | 756 | } |
wuliqunyy | 0:be95bfb06686 | 757 | |
wuliqunyy | 0:be95bfb06686 | 758 | if (validData == 1) { |
wuliqunyy | 0:be95bfb06686 | 759 | /* valid */ |
wuliqunyy | 0:be95bfb06686 | 760 | token->type = SCPI_TOKEN_ARBITRARY_BLOCK_PROGRAM_DATA; |
wuliqunyy | 0:be95bfb06686 | 761 | } else if (validData == 0) { |
wuliqunyy | 0:be95bfb06686 | 762 | /* incomplete */ |
wuliqunyy | 0:be95bfb06686 | 763 | token->type = SCPI_TOKEN_UNKNOWN; |
wuliqunyy | 0:be95bfb06686 | 764 | token->len = 0; |
wuliqunyy | 0:be95bfb06686 | 765 | state->pos = state->buffer + state->len; |
wuliqunyy | 0:be95bfb06686 | 766 | } else { |
wuliqunyy | 0:be95bfb06686 | 767 | /* invalid */ |
wuliqunyy | 0:be95bfb06686 | 768 | token->type = SCPI_TOKEN_UNKNOWN; |
wuliqunyy | 0:be95bfb06686 | 769 | state->pos = token->ptr; |
wuliqunyy | 0:be95bfb06686 | 770 | token->len = 0; |
wuliqunyy | 0:be95bfb06686 | 771 | } |
wuliqunyy | 0:be95bfb06686 | 772 | |
wuliqunyy | 0:be95bfb06686 | 773 | return token->len + (token->ptr - ptr); |
wuliqunyy | 0:be95bfb06686 | 774 | } |
wuliqunyy | 0:be95bfb06686 | 775 | |
wuliqunyy | 0:be95bfb06686 | 776 | /* 7.7.7 <EXPRESSION PROGRAM DATA> */ |
wuliqunyy | 0:be95bfb06686 | 777 | static int isProgramExpression(int c) { |
wuliqunyy | 0:be95bfb06686 | 778 | if ((c >= 0x20) && (c <= 0x7e)) { |
wuliqunyy | 0:be95bfb06686 | 779 | if ((c != '"') |
wuliqunyy | 0:be95bfb06686 | 780 | && (c != '#') |
wuliqunyy | 0:be95bfb06686 | 781 | && (c != '\'') |
wuliqunyy | 0:be95bfb06686 | 782 | && (c != '(') |
wuliqunyy | 0:be95bfb06686 | 783 | && (c != ')') |
wuliqunyy | 0:be95bfb06686 | 784 | && (c != ';')) { |
wuliqunyy | 0:be95bfb06686 | 785 | return 1; |
wuliqunyy | 0:be95bfb06686 | 786 | } |
wuliqunyy | 0:be95bfb06686 | 787 | } |
wuliqunyy | 0:be95bfb06686 | 788 | |
wuliqunyy | 0:be95bfb06686 | 789 | return 0; |
wuliqunyy | 0:be95bfb06686 | 790 | } |
wuliqunyy | 0:be95bfb06686 | 791 | |
wuliqunyy | 0:be95bfb06686 | 792 | static void skipProgramExpression(lex_state_t * state) { |
wuliqunyy | 0:be95bfb06686 | 793 | while (!iseos(state) && isProgramExpression(state->pos[0])) { |
wuliqunyy | 0:be95bfb06686 | 794 | state->pos++; |
wuliqunyy | 0:be95bfb06686 | 795 | } |
wuliqunyy | 0:be95bfb06686 | 796 | } |
wuliqunyy | 0:be95bfb06686 | 797 | |
wuliqunyy | 0:be95bfb06686 | 798 | /* TODO: 7.7.7.2-2 recursive - any program data */ |
wuliqunyy | 0:be95bfb06686 | 799 | |
wuliqunyy | 0:be95bfb06686 | 800 | /** |
wuliqunyy | 0:be95bfb06686 | 801 | * Detect token Expression |
wuliqunyy | 0:be95bfb06686 | 802 | * @param state |
wuliqunyy | 0:be95bfb06686 | 803 | * @param token |
wuliqunyy | 0:be95bfb06686 | 804 | * @return |
wuliqunyy | 0:be95bfb06686 | 805 | */ |
wuliqunyy | 0:be95bfb06686 | 806 | int scpiLex_ProgramExpression(lex_state_t * state, scpi_token_t * token) { |
wuliqunyy | 0:be95bfb06686 | 807 | token->ptr = state->pos; |
wuliqunyy | 0:be95bfb06686 | 808 | |
wuliqunyy | 0:be95bfb06686 | 809 | if (!iseos(state) && ischr(state, '(')) { |
wuliqunyy | 0:be95bfb06686 | 810 | state->pos++; |
wuliqunyy | 0:be95bfb06686 | 811 | skipProgramExpression(state); |
wuliqunyy | 0:be95bfb06686 | 812 | |
wuliqunyy | 0:be95bfb06686 | 813 | if (!iseos(state) && ischr(state, ')')) { |
wuliqunyy | 0:be95bfb06686 | 814 | state->pos++; |
wuliqunyy | 0:be95bfb06686 | 815 | token->len = state->pos - token->ptr; |
wuliqunyy | 0:be95bfb06686 | 816 | } else { |
wuliqunyy | 0:be95bfb06686 | 817 | token->len = 0; |
wuliqunyy | 0:be95bfb06686 | 818 | } |
wuliqunyy | 0:be95bfb06686 | 819 | } |
wuliqunyy | 0:be95bfb06686 | 820 | |
wuliqunyy | 0:be95bfb06686 | 821 | if ((token->len > 0)) { |
wuliqunyy | 0:be95bfb06686 | 822 | token->type = SCPI_TOKEN_PROGRAM_EXPRESSION; |
wuliqunyy | 0:be95bfb06686 | 823 | } else { |
wuliqunyy | 0:be95bfb06686 | 824 | token->type = SCPI_TOKEN_UNKNOWN; |
wuliqunyy | 0:be95bfb06686 | 825 | state->pos = token->ptr; |
wuliqunyy | 0:be95bfb06686 | 826 | token->len = 0; |
wuliqunyy | 0:be95bfb06686 | 827 | } |
wuliqunyy | 0:be95bfb06686 | 828 | |
wuliqunyy | 0:be95bfb06686 | 829 | return token->len; |
wuliqunyy | 0:be95bfb06686 | 830 | } |
wuliqunyy | 0:be95bfb06686 | 831 | |
wuliqunyy | 0:be95bfb06686 | 832 | /** |
wuliqunyy | 0:be95bfb06686 | 833 | * Detect token comma |
wuliqunyy | 0:be95bfb06686 | 834 | * @param state |
wuliqunyy | 0:be95bfb06686 | 835 | * @param token |
wuliqunyy | 0:be95bfb06686 | 836 | * @return |
wuliqunyy | 0:be95bfb06686 | 837 | */ |
wuliqunyy | 0:be95bfb06686 | 838 | int scpiLex_Comma(lex_state_t * state, scpi_token_t * token) { |
wuliqunyy | 0:be95bfb06686 | 839 | token->ptr = state->pos; |
wuliqunyy | 0:be95bfb06686 | 840 | |
wuliqunyy | 0:be95bfb06686 | 841 | if (skipChr(state, ',')) { |
wuliqunyy | 0:be95bfb06686 | 842 | token->len = 1; |
wuliqunyy | 0:be95bfb06686 | 843 | token->type = SCPI_TOKEN_COMMA; |
wuliqunyy | 0:be95bfb06686 | 844 | } else { |
wuliqunyy | 0:be95bfb06686 | 845 | token->len = 0; |
wuliqunyy | 0:be95bfb06686 | 846 | token->type = SCPI_TOKEN_UNKNOWN; |
wuliqunyy | 0:be95bfb06686 | 847 | } |
wuliqunyy | 0:be95bfb06686 | 848 | |
wuliqunyy | 0:be95bfb06686 | 849 | return token->len; |
wuliqunyy | 0:be95bfb06686 | 850 | } |
wuliqunyy | 0:be95bfb06686 | 851 | |
wuliqunyy | 0:be95bfb06686 | 852 | /** |
wuliqunyy | 0:be95bfb06686 | 853 | * Detect token semicolon |
wuliqunyy | 0:be95bfb06686 | 854 | * @param state |
wuliqunyy | 0:be95bfb06686 | 855 | * @param token |
wuliqunyy | 0:be95bfb06686 | 856 | * @return |
wuliqunyy | 0:be95bfb06686 | 857 | */ |
wuliqunyy | 0:be95bfb06686 | 858 | int scpiLex_Semicolon(lex_state_t * state, scpi_token_t * token) { |
wuliqunyy | 0:be95bfb06686 | 859 | token->ptr = state->pos; |
wuliqunyy | 0:be95bfb06686 | 860 | |
wuliqunyy | 0:be95bfb06686 | 861 | if (skipChr(state, ';')) { |
wuliqunyy | 0:be95bfb06686 | 862 | token->len = 1; |
wuliqunyy | 0:be95bfb06686 | 863 | token->type = SCPI_TOKEN_SEMICOLON; |
wuliqunyy | 0:be95bfb06686 | 864 | } else { |
wuliqunyy | 0:be95bfb06686 | 865 | token->len = 0; |
wuliqunyy | 0:be95bfb06686 | 866 | token->type = SCPI_TOKEN_UNKNOWN; |
wuliqunyy | 0:be95bfb06686 | 867 | } |
wuliqunyy | 0:be95bfb06686 | 868 | |
wuliqunyy | 0:be95bfb06686 | 869 | return token->len; |
wuliqunyy | 0:be95bfb06686 | 870 | } |
wuliqunyy | 0:be95bfb06686 | 871 | |
wuliqunyy | 0:be95bfb06686 | 872 | /** |
wuliqunyy | 0:be95bfb06686 | 873 | * Detect token colon |
wuliqunyy | 0:be95bfb06686 | 874 | * @param state |
wuliqunyy | 0:be95bfb06686 | 875 | * @param token |
wuliqunyy | 0:be95bfb06686 | 876 | * @return |
wuliqunyy | 0:be95bfb06686 | 877 | */ |
wuliqunyy | 0:be95bfb06686 | 878 | int scpiLex_Colon(lex_state_t * state, scpi_token_t * token) { |
wuliqunyy | 0:be95bfb06686 | 879 | token->ptr = state->pos; |
wuliqunyy | 0:be95bfb06686 | 880 | |
wuliqunyy | 0:be95bfb06686 | 881 | if (skipChr(state, ':')) { |
wuliqunyy | 0:be95bfb06686 | 882 | token->len = 1; |
wuliqunyy | 0:be95bfb06686 | 883 | token->type = SCPI_TOKEN_COLON; |
wuliqunyy | 0:be95bfb06686 | 884 | } else { |
wuliqunyy | 0:be95bfb06686 | 885 | token->len = 0; |
wuliqunyy | 0:be95bfb06686 | 886 | token->type = SCPI_TOKEN_UNKNOWN; |
wuliqunyy | 0:be95bfb06686 | 887 | } |
wuliqunyy | 0:be95bfb06686 | 888 | |
wuliqunyy | 0:be95bfb06686 | 889 | return token->len; |
wuliqunyy | 0:be95bfb06686 | 890 | } |
wuliqunyy | 0:be95bfb06686 | 891 | |
wuliqunyy | 0:be95bfb06686 | 892 | /** |
wuliqunyy | 0:be95bfb06686 | 893 | * Detect specified character |
wuliqunyy | 0:be95bfb06686 | 894 | * @param state |
wuliqunyy | 0:be95bfb06686 | 895 | * @param token |
wuliqunyy | 0:be95bfb06686 | 896 | * @return |
wuliqunyy | 0:be95bfb06686 | 897 | */ |
wuliqunyy | 0:be95bfb06686 | 898 | int scpiLex_SpecificCharacter(lex_state_t * state, scpi_token_t * token, char chr) { |
wuliqunyy | 0:be95bfb06686 | 899 | token->ptr = state->pos; |
wuliqunyy | 0:be95bfb06686 | 900 | |
wuliqunyy | 0:be95bfb06686 | 901 | if (skipChr(state, chr)) { |
wuliqunyy | 0:be95bfb06686 | 902 | token->len = 1; |
wuliqunyy | 0:be95bfb06686 | 903 | token->type = SCPI_TOKEN_SPECIFIC_CHARACTER; |
wuliqunyy | 0:be95bfb06686 | 904 | } else { |
wuliqunyy | 0:be95bfb06686 | 905 | token->len = 0; |
wuliqunyy | 0:be95bfb06686 | 906 | token->type = SCPI_TOKEN_UNKNOWN; |
wuliqunyy | 0:be95bfb06686 | 907 | } |
wuliqunyy | 0:be95bfb06686 | 908 | |
wuliqunyy | 0:be95bfb06686 | 909 | return token->len; |
wuliqunyy | 0:be95bfb06686 | 910 | } |
wuliqunyy | 0:be95bfb06686 | 911 | |
wuliqunyy | 0:be95bfb06686 | 912 | /** |
wuliqunyy | 0:be95bfb06686 | 913 | * Detect token New line |
wuliqunyy | 0:be95bfb06686 | 914 | * @param state |
wuliqunyy | 0:be95bfb06686 | 915 | * @param token |
wuliqunyy | 0:be95bfb06686 | 916 | * @return |
wuliqunyy | 0:be95bfb06686 | 917 | */ |
wuliqunyy | 0:be95bfb06686 | 918 | int scpiLex_NewLine(lex_state_t * state, scpi_token_t * token) { |
wuliqunyy | 0:be95bfb06686 | 919 | token->ptr = state->pos; |
wuliqunyy | 0:be95bfb06686 | 920 | |
wuliqunyy | 0:be95bfb06686 | 921 | skipChr(state, '\r'); |
wuliqunyy | 0:be95bfb06686 | 922 | skipChr(state, '\n'); |
wuliqunyy | 0:be95bfb06686 | 923 | |
wuliqunyy | 0:be95bfb06686 | 924 | token->len = state->pos - token->ptr; |
wuliqunyy | 0:be95bfb06686 | 925 | |
wuliqunyy | 0:be95bfb06686 | 926 | if ((token->len > 0)) { |
wuliqunyy | 0:be95bfb06686 | 927 | token->type = SCPI_TOKEN_NL; |
wuliqunyy | 0:be95bfb06686 | 928 | } else { |
wuliqunyy | 0:be95bfb06686 | 929 | token->type = SCPI_TOKEN_UNKNOWN; |
wuliqunyy | 0:be95bfb06686 | 930 | state->pos = token->ptr; |
wuliqunyy | 0:be95bfb06686 | 931 | token->len = 0; |
wuliqunyy | 0:be95bfb06686 | 932 | } |
wuliqunyy | 0:be95bfb06686 | 933 | |
wuliqunyy | 0:be95bfb06686 | 934 | return token->len; |
wuliqunyy | 0:be95bfb06686 | 935 | } |
wuliqunyy | 0:be95bfb06686 | 936 |