scpi_parser.c 160 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606
  1. #define SCPI_PARSER_C
  2. #include "app/scpi/scpi_parser.h"
  3. #include "app/scpi/scpi_core.h"
  4. #include "app/scpi/scpi_numeric.h"
  5. #include "my_assert.h"
  6. #include <ctype.h> // isdigit(), isalpha(), isalnum()
  7. //----------------------------------------------------------------
  8. // Refer to:
  9. // [1] IEEE 488.2 Standard, revision IEEE Std 488.2-1987 (1992)
  10. // "IEEE Standard Codes, Formats, Protocols, and Common Commands for Use With IEEE Std 488.1-1987, IEEE
  11. // Standard Digital Interface for Programmable Instrumentation"
  12. // [2] SCPI Specification, revision 1999.0
  13. // "Standard Commands for Programmable Instruments (SCPI), VERSION 1999.0, May 1999"
  14. //----------------------------------------------------------------
  15. // =================================================================================================================
  16. // Notes:
  17. /* White Space
  18. 7.4.1.2 Encoding Syntax, [1]
  19. <white-space character> is de?ned as a single ASCII-encoded byte in the range of 00-09, 0B-20 (0-9, 11-32 decimal).
  20. This range includes the ASCII control characters and the space but excludes the newline.
  21. New Line
  22. 7.5 <PROGRAM MESSAGE TERMINATOR>, [1]
  23. NL is defined as a single ASCII-encoded byte 0A (10 decimal).
  24. NOTE: An END message cannot be sent without an accompanying data byte. See IEEE Std 488.1-1987 [4]. The ^END syntactic
  25. element implies the IEEE 488.1 END message is sent with the last data byte of the preceding syntactic element.
  26. Elements Summary
  27. 7.3.3 Functional Element Summary, [1]
  28. */
  29. // =================================================================================================================
  30. // @sParserEntryPoint_t
  31. // Parser context backup/restore point data structure
  32. // see @parserBackupContext and @parserRestoreContext
  33. typedef struct
  34. {
  35. const char * str; // temportary buffer iterator, shall not be accessed by user.
  36. const char * head; // the head of the buffer, points to the begining of the entity after
  37. // ... the call of any paraser-function.
  38. const char * tail; // the tail of the buffer, points to the end of entity after the call
  39. // ... of any paraser-function.
  40. int32_t value; // Generic-Purpose integer value
  41. }
  42. sParserEntryPoint_t;
  43. typedef struct
  44. {
  45. struct // common context
  46. {
  47. int32_t error; // parser error code
  48. const char * pErrorMsg; // parser error message
  49. const char * str; // temportary buffer iterator, shall not be accessed by user.
  50. /* --- unused, the (@tail - @head) expression is used instead
  51. size_t length; // input/output field, specifies the length of the buffer before
  52. // ... calling the parser-function, and holds the length of parsed
  53. // ... element after the call.
  54. */
  55. const char * head; // the head of the buffer, points to the begining of the entity after
  56. // ... the call of any paraser-function.
  57. const char * tail; // the tail of the buffer, points to the end of entity after the call
  58. // ... of any paraser-function.
  59. bool bEndMessage; // End-of-message indicator. Specifies if the specified buffer holds
  60. // ... a complete message or not. If the @str points to a portion
  61. // ... of message's data and there no information about the whole
  62. // ... message length, the @bEndMessage value is 'false'. Otherwise,
  63. // ... if known that the @str buffer contains the final portion of
  64. // ... data (inclidung the terminating characters, maybe), the value
  65. // ... is 'true'.
  66. bool bContextKept; // Clean context indicator. If @prepareParserContext has been called
  67. // ... with keeping context, this variable is 'true'. Otherwise, if
  68. // ... @prepareParserContext has been called with cleaning the context
  69. // ... then this variable is 'false'.
  70. uint8_t type; // the entity type, encoded as described in @eScpiEntityType_t enum.
  71. };
  72. sParserEntryPoint_t parseNumber_xSaveObj; // dedicated parser backup/restore structure for @parseNumber routine
  73. union // private context union
  74. {
  75. struct // private context for @parseString function
  76. {
  77. bool QuoteOpened; // Quote-opened indicator: used to help to interpretate each quote
  78. // ... either as an opening or closing one.
  79. char QuoteSignature; // Quote signature: can be either '\"' or '\''
  80. char LastCharacter; // The temporary character buffer to store the last processed byte
  81. } parseString;
  82. struct // private context for @parseArbitraryBlock function
  83. {
  84. size_t LengthDigits; // the number of demical digits used to encode the actual length
  85. // ... of the block
  86. uint32_t Length; // Actual length of the block in bytes, encoded as demical value
  87. // ... in range 0..999999999
  88. bool KeySign; // start-character indicator: used to determine if the starting
  89. // ... character have already processed or not yet.
  90. bool DataStarted; // mode indicator: used to determine if all block's header characters
  91. // ... have already processed and the current character is a part of payload.
  92. } parseArbitraryBlock;
  93. struct // private context for @parseProgramMnemonic function
  94. {
  95. size_t CharsNumber; // amount of characters in the mnemonic.
  96. bool FirstAlpha; // start-character indicator: used to determine if the first alpha-
  97. // ...-character has been processed or not.
  98. } parseProgramMnemonic;
  99. struct // private context for @parseCommandProgramHeader function
  100. {
  101. // NOTE: @parseCommandProgramHeader function uses the @parseProgramMnemonic
  102. // ... calls as a part of implementation, thus it is required to keep
  103. // ... the @parseProgramMnemonic's context valid!
  104. const size_t CharsNumber; // reserved, keep unchanged (@parseProgramMnemonic)
  105. const bool FirstAlpha; // reserved, keep unchanged (@parseProgramMnemonic)
  106. bool LastCallContinue; // last call context indicator: shows if it is required to continue
  107. // ... the last call context or it is needed to reset it.
  108. bool LastCallSuccess; // last call success indicator: shows if the last call succeded or not.
  109. bool CommonForm; // common form indicator: shows if the command program header have a
  110. // ... common form (begins from askterisk, 7.6.1.2 Encoding Syntax, [1])
  111. const char * _head; // cached @head value
  112. const char * _tail; // cached @tail value
  113. char LastCharacter; // the last processed character
  114. } parseCommandProgramHeader;
  115. struct // private context for @parseDemicalNumber function
  116. {
  117. bool dotAllowed; // dot-character indicator: shows if the character is
  118. // ... allowed in current state;
  119. bool signAllowed; // sign-character indicator: shows if the character is
  120. // ... allowed in current state;
  121. bool digitAllowed; // digit-character indicator: shows if the character is
  122. // ... allowed in current state;
  123. bool whiteAllowed; // white-character indicator: shows if the character is
  124. // ... allowed in current state;
  125. bool expAllowed; // exponent-character indicator: shows if the character is
  126. // ... allowed in current state;
  127. bool termAllowed; // unit terminator indicator: shows if a group of terminating characters are
  128. // ... allowed in current state;
  129. bool manitssaOk; // mantissa indicator: shows if mantissa received or not.
  130. bool exponentOk; // exponent indicator: shows if exponent received or not.
  131. bool dotFound; // dot-character indicator: show if the dot character is faced in mantissa
  132. bool parseStage; // the parser state:
  133. // false - search for mantissa;
  134. // true - search for exponent;
  135. } parseDemicalNumber;
  136. struct // private context for @parseNonDemicalNumber function
  137. {
  138. size_t digits; // number of digits processed
  139. bool allow0_1; // allow binary characters ( 0..1 )
  140. bool allow0_7; // allow octal characters ( 0..7 )
  141. bool allow0_f; // allow hex characters ( 0..9, a-f )
  142. bool allowNumChar; // allow numeric-system designator character
  143. bool foundNumericChar; // numeric character indicator (#)
  144. } parseNonDemicalNumber;
  145. struct // private context for @parseProgramDataSeparator function
  146. {
  147. bool sepFound; // indicator showing if separator-character found or not
  148. } parseProgramDataSeparator;
  149. struct // private context for @parseProgramHeaderSeparator function
  150. {
  151. bool sepFound; // indicator showing if separator-character found or not
  152. } parseProgramHeaderSeparator;
  153. struct // private context for @parseProgramMessageSeparator function
  154. {
  155. bool sepFound; // indicator showing if separator-character found or not
  156. } parseProgramMessageSeparator;
  157. struct // private context for @parseEndOfCommand function
  158. {
  159. bool sepFound; // indicator showing if separator-character found or not
  160. } parseEndOfCommand;
  161. struct // private context for @parseWhiteSequence function
  162. {
  163. bool whiteFound; // indicator showing if separator-character found or not
  164. bool nlFound; // indicator showing if NL-character found or not
  165. } parseWhiteSequence;
  166. };
  167. }
  168. sParseEntry_t;
  169. // =================================================================================================================
  170. STATIC_ASSERT( sizeof(xParseEntry_t) == sizeof(sParseEntry_t), "Invalid xParseEntry_t/sParseEntry_t size" );
  171. // =================================================================================================================
  172. // @parserBackupContext
  173. // Internal only function.
  174. // Saves the parser entry point before the parsing to give ability to restore the context iterators after
  175. // unsuccessful parsing.
  176. // Does not guaranee full context backup.
  177. // @pObj = parser context;
  178. // @to = parser backup structure
  179. static inline void parserBackupContext( const sParseEntry_t * pObj, sParserEntryPoint_t * to )
  180. {
  181. my_assert( pObj );
  182. my_assert( to );
  183. to->head = pObj->head;
  184. to->tail = pObj->tail;
  185. to->str = pObj->str;
  186. }
  187. // @parserBackupContextValue
  188. // Internal only function.
  189. // Saves parser-dependent general puspose value
  190. // @to = parser backup structure
  191. // @value = generic purpose value
  192. static inline void parserBackupContextValue( sParserEntryPoint_t * to, int32_t value )
  193. {
  194. my_assert( to );
  195. to->value = value;
  196. }
  197. // @parserRestoreContext
  198. // Internal only function.
  199. // Loads the parser entry point after the one has been saved by @parserBackupContext to
  200. // restore parser entry point after unsuccessful parsing.
  201. // Does not guaranee full context restore.
  202. // @pObj = parser context to restore to;
  203. // @from = parser restore structure
  204. // NOTE: resets @bContextKept variable to prevent using invalid data
  205. static inline void parserRestoreContext( sParseEntry_t * pObj, const sParserEntryPoint_t * from )
  206. {
  207. my_assert( from );
  208. pObj->head = from->head;
  209. pObj->tail = from->tail;
  210. pObj->str = from->str;
  211. // reset context keeping indicator to reset the next parser's internal variables
  212. pObj->bContextKept = false;
  213. }
  214. // @parserRestoreContext
  215. // Internal only function.
  216. // Loads the parser dependent general purpose value after the one has been saved by @parserBackupContextValue
  217. // @pObj = parser context to restore to, optional, can be NULL;
  218. // @from = parser restore structure
  219. // @pvalue = pointer to the variable to load generic purpose value saved by @parserBackupContext in 'value' argument,
  220. // ... optional, can be NULL
  221. static inline void parserRestoreContextValue( const sParserEntryPoint_t * from, int32_t * pvalue )
  222. {
  223. my_assert( from );
  224. my_assert( pvalue );
  225. *pvalue = from->value;
  226. }
  227. // =================================================================================================================
  228. // @scpi_isalnum
  229. // Detects if the passed character @ch is ASCII alpha-numeric character
  230. // Refer to:
  231. // "7.6.1 <COMMAND PROGPRAM HEADER>", [1]
  232. // "7.6.1.2 Encoding Syntax", [1]
  233. // Note: the implementation is depending on the current locale.
  234. // This implementation uses the default functions from the standard
  235. // ...<ctype>-header.
  236. static inline bool scpi_isalnum( char ch )
  237. {
  238. // for the default locale only A-Za-z0-9 are allowed
  239. return isalnum(ch);
  240. }
  241. // =================================================================================================================
  242. // @scpi_isalpha
  243. // Detects if the passed character @ch is ASCII alpha character
  244. // Refer to:
  245. // "7.6.1 <COMMAND PROGPRAM HEADER>", [1]
  246. // "7.6.1.2 Encoding Syntax", [1]
  247. // Note: the implementation is depending on the current locale.
  248. // This implementation uses the default functions from the standard
  249. // ...<ctype>-header.
  250. static inline bool scpi_isalpha( char ch )
  251. {
  252. // for the default locale only A-Za-z0-9 are allowed
  253. return isalpha(ch);
  254. }
  255. // =================================================================================================================
  256. // @scpi_isdigit
  257. // Detects if the passed character @ch is ASCII digit character
  258. // Refer to:
  259. // "7.6.1 <COMMAND PROGPRAM HEADER>", [1]
  260. // "7.6.1.2 Encoding Syntax", [1]
  261. static inline bool scpi_isdigit( char ch )
  262. {
  263. // for the locale does not affect to the implementation, only 0-9 are allowed
  264. return isdigit(ch);
  265. }
  266. // =================================================================================================================
  267. // @scpi_ishex
  268. // Detects if the passed character @ch is a HEX character (0..9, A-F, a-f)
  269. static inline bool scpi_ishex( char ch )
  270. {
  271. // for the locale does not affect to the implementation, only 0-9 are allowed
  272. return isdigit(ch) || ( 'a' <= ch && 'f' >= ch ) || ( 'A' <= ch && 'F' >= ch );
  273. }
  274. // =================================================================================================================
  275. // @prepareParserContext
  276. // Preapres a parser context @xObj before a parser-function call.
  277. // The function must be called before each call of any parser-function.
  278. // If it is required to call the parser function multiple times, user
  279. // ... shall analyze the last return status and specify the @bKeepContext
  280. // ... parameter to the corresponding value. See also @eScpiParserStatus_t.
  281. // Parameters:
  282. // @xObj - the parse context to prepare;
  283. // @pBuffer - the memory buffer containing the SCPI element to be parsed;
  284. // @length - the memory buffer length;
  285. // @bEndMessage - specifies if the specified buffer holds a complete
  286. // ... message or not. If the @pBuffer contains a portion of received
  287. // ... data and there no information about the whole message length,
  288. // ... the @bEndMessage value must be 'false'. Otherwise, if known that
  289. // ... the buffer contains the final portion of data (even inclidung the
  290. // ... terminating characters), the value must be 'true'.
  291. // @bKeepContext - specifies if it is required to cleanup the context
  292. // ... to restart the parsing ('false'), or it is required to keep the
  293. // ... context to continue ('true').
  294. // Returns: 'true' in success case,
  295. // 'false' otherwise.
  296. // Note in case the function returns 'false' user must not use the
  297. // ... @xObj context due to it is still not prepared.
  298. bool prepareParserContext( xParseEntry_t * xObj,
  299. const void * pBuffer,
  300. size_t length,
  301. bool bEndMessage,
  302. bool bKeepContext )
  303. {
  304. sParseEntry_t * pObj = (sParseEntry_t*)xObj;
  305. if( NULL != pObj && NULL != pBuffer && 0 != length )
  306. {
  307. if( !bKeepContext )
  308. {
  309. // Cleanup the private context
  310. memset( pObj, 0, sizeof(sParseEntry_t) );
  311. pObj->bContextKept = false;
  312. }
  313. else
  314. {
  315. pObj->bContextKept = true;
  316. }
  317. //pObj->length = length; // init the buffer length
  318. pObj->head = (const char*)pBuffer; // save the buffer start
  319. pObj->tail = pObj->head + length; // save the buffer end
  320. pObj->str = pObj->head; // init the @str iterator to the beginning of the buffer
  321. pObj->bEndMessage = bEndMessage; // save end-of-message flag
  322. pObj->type = eScpiEntityTypeInvalid; // forward type set: invalid (default)
  323. pObj->error = SCPI_ERROR_SUCCESS; // reset error code
  324. pObj->pErrorMsg = NULL; // reset error message
  325. return true;
  326. }
  327. return false;
  328. }
  329. // =================================================================================================================
  330. // @getParsedEntityDetails
  331. // Retrieves the parsing results from the parser context.
  332. // The function shall not be called until either the
  333. // ... @eScpiParserStatus_success status or @eScpiParserStatus_need_data
  334. // ... are returned by last parser-function call.
  335. // Note: take in account, that in case @eScpiParserStatus_need_data had
  336. // ... been returned during the last parser-function call, the @eEntityType
  337. // ... will be set to @eScpiEntityTypeInvalid due to the entity is still
  338. // ... not identified (need more data).
  339. // Parameters:
  340. // @xObj - the parser context to get results from;
  341. // @pxEntityBegin - pointer to the pointer-variable to store the
  342. // ... parsed entity beginning, can be NULL;
  343. // @pxEntityEnd - pointer to the pointer-variable to store the
  344. // ... parsed entity ending, can be NULL;
  345. // @peEntityType - pointer to the uint8-variable to store the
  346. // ... parsed entity type, the type is encoded as described
  347. // ... in @eScpiEntityType_t enum, can be NULL;
  348. // Note: in case the parameters described above are NULL an appropriate
  349. // ... information will not be returned and can be retrieved by
  350. // ... another call of this function with non-NULL argument until
  351. // ... the @prepareParserContext is called for the specified context.
  352. // Returns: 'true' in success case,
  353. // 'false' otherwise.
  354. bool getParsedEntityDetails( const xParseEntry_t * xObj,
  355. const void * * pxEntityBegin,
  356. const void * * pxEntityEnd,
  357. uint8_t * peEntityType )
  358. {
  359. bool status = false;
  360. if( NULL != xObj )
  361. {
  362. if( NULL != pxEntityBegin )
  363. {
  364. *pxEntityBegin = ((const sParseEntry_t*)xObj)->head;
  365. status = status || true;
  366. }
  367. if( NULL != pxEntityEnd )
  368. {
  369. *pxEntityEnd = ((const sParseEntry_t*)xObj)->tail;
  370. status = status || true;
  371. }
  372. if( NULL != peEntityType )
  373. {
  374. *peEntityType = ((const sParseEntry_t*)xObj)->type;
  375. status = status || true;
  376. }
  377. }
  378. my_assert( status );
  379. return status;
  380. }
  381. // =================================================================================================================
  382. // @getParserError
  383. // Retrieves the last parser error code and message
  384. // The function shall not be called until either the @eScpiParserStatus_failed status
  385. // is returned by last parser-function call.
  386. // @xObj - the parser context to get results from;
  387. // @pErrorCode - optional pointer to the variable to store the error code, can be NULL;
  388. // Returns:
  389. // The parser error message string. Use @getParsedEntityDetails to retrieve more details.
  390. // Possible values:
  391. // - NULL: no error occurred in specified context or invalid context;
  392. // - non-null: valid constant null-terminated string message, no memory free is required;
  393. const char * getParserError( const xParseEntry_t * xObj,
  394. int32_t * pErrorCode )
  395. {
  396. if( NULL != xObj )
  397. {
  398. if( NULL != pErrorCode )
  399. {
  400. *pErrorCode = ((const sParseEntry_t*)xObj)->error;
  401. }
  402. return ((const sParseEntry_t*)xObj)->pErrorMsg;
  403. }
  404. return NULL;
  405. }
  406. // =================================================================================================================
  407. // @setParserError
  408. // Assigne the last parser error code and message
  409. // The function shall not be called after calling any parser function that return error
  410. // status code to prevent overwriting actual error code and description.
  411. // @xObj - the parser context to get results from;
  412. // @errorCode - SCPI Error code, [2]
  413. // @pErrorDesc - Error description, constant null-terminated string;
  414. // Returns:
  415. // true - successful operation,
  416. // false - failed
  417. bool setParserError( const xParseEntry_t * xObj,
  418. int32_t errorCode,
  419. const char * pErrorDesc )
  420. {
  421. my_assert( xObj );
  422. if( NULL != xObj )
  423. {
  424. ((sParseEntry_t*)xObj)->error = errorCode;
  425. ((sParseEntry_t*)xObj)->pErrorMsg = pErrorDesc;
  426. return true;
  427. }
  428. return false;
  429. }
  430. // =================================================================================================================
  431. // @parseArbitraryBlock
  432. // "Parser-function", parses an arbitrary block (ARBITRARY BLOCK PROGRAM DATA)
  433. // References:
  434. // "7.7.6 <ARBITRARY BLOCK PROGRAM DATA>", [1]
  435. // "7.7.6.2 Encoding Syntax", [1]
  436. // Parameters:
  437. // @xObj - parser context, must be prepared by @prepareParserContext
  438. // ... before each call. If it is needed to continue the processing, the
  439. // ... @bKeepContext parameter duing the call @prepareParserContext must
  440. // ... be set to 'true', and otherwise, to restart parsing the parameter
  441. // ... must be set to 'false'.
  442. // Returns:
  443. // eScpiParserStatus_failed - error, can not parse data;
  444. // eScpiParserStatus_success - success, the string has been successfully parsed;
  445. // eScpiParserStatus_need_data - warning, the string can not be parsed due to
  446. // there no enough data to process.
  447. eScpiParserStatus_t parseArbitraryBlock( xParseEntry_t * xObj )
  448. {
  449. sParseEntry_t * pObj = (sParseEntry_t*)xObj;
  450. eScpiParserStatus_t status = eScpiParserStatus_invalid;
  451. if( NULL != pObj && NULL != pObj->str && 0 != ((ptrdiff_t)pObj->tail - (ptrdiff_t)pObj->head)
  452. && (0 < ((ptrdiff_t)pObj->tail - (ptrdiff_t)pObj->str)) )
  453. {
  454. status = eScpiParserStatus_failed; // forward status set: the default 'eScpiParserStatus_invalid' shall
  455. // ... not be returned due to the context will be changed.
  456. // if the context has been cleaned up
  457. if( ! pObj->bContextKept )
  458. {
  459. // initialize private context variables
  460. pObj->parseArbitraryBlock.KeySign = false;
  461. pObj->parseArbitraryBlock.DataStarted = false;
  462. pObj->parseArbitraryBlock.LengthDigits = 0;
  463. pObj->parseArbitraryBlock.Length = 0;
  464. }
  465. // walk til the end of the buffer
  466. while( pObj->str != pObj->tail )
  467. {
  468. char character = *(pObj->str++);
  469. if( ! pObj->parseArbitraryBlock.KeySign )
  470. {
  471. if( '#' == character ) // !: starting character
  472. {
  473. pObj->parseArbitraryBlock.KeySign = true; // starting character found
  474. }
  475. else
  476. {
  477. pObj->error = SCPI_ERROR_INVALID_BLOCKDATA;
  478. pObj->pErrorMsg = SCPI_ERROR_INVALID_BLOCKDATA_MSG;
  479. status = eScpiParserStatus_failed; // error: the data does not start from the starting character
  480. break;
  481. }
  482. }
  483. else
  484. {
  485. // check if the data payload is already being processed:
  486. if( ! pObj->parseArbitraryBlock.DataStarted )
  487. {
  488. // nope, need to process the header
  489. // check if the number of digits of the length-entity is loaded
  490. if( 0 == pObj->parseArbitraryBlock.LengthDigits )
  491. {
  492. // nope, need to load a number of digits
  493. // check if the current character is a digit:
  494. if( ! scpi_isdigit( character ) )
  495. {
  496. pObj->error = SCPI_ERROR_INVALID_BLOCKDATA;
  497. pObj->pErrorMsg = SCPI_ERROR_INVALID_BLOCKDATA_MSG;
  498. // nope, it is a fiasco
  499. status = eScpiParserStatus_failed; // error: invalid data block header
  500. break;
  501. }
  502. else // true == scpi_isdigit(character)
  503. {
  504. // yep, load the value
  505. pObj->parseArbitraryBlock.LengthDigits = (size_t)(character - '0');
  506. // check the number loaded:
  507. if( 0 == pObj->parseArbitraryBlock.LengthDigits )
  508. {
  509. // indefnite format: the data block must be terminated by the
  510. // ... couple of end-of-line and end-of-message terminators (NL + ^END).
  511. // In case there no way to detemine the length of the payload,
  512. // ... the length field must be set to zero and function shall return
  513. // ... the warining status "eScpiParserStatus_need_data". Otherwise, in case
  514. // ... it is defenitely known that this portion of data is the last one,
  515. // ... the function shall not to change the length @Length (it is already
  516. // ... set to the passed buffer length) and return success status.
  517. if( pObj->bEndMessage )
  518. {
  519. pObj->type = eScpiEntityTypeArbitraryBlock; // assign the entity type
  520. // An arbirary block takes the whole buffer, the @tail pointer refers to the
  521. // ... end of the buffer. The @tail and @head pointers specify the entity length as
  522. // ... whole buffer length.
  523. status = eScpiParserStatus_success; // success: an arbirary block takes the whole buffer
  524. }
  525. else
  526. {
  527. (void)pObj->type; // do not assign the entity type, it is not identified yet.
  528. // No information about the whole message length on this level: return "eScpiParserStatus_need_data"
  529. pObj->tail = pObj->head; // set to zero length to indicate "indefine format" by
  530. // ... assigning the @head to the @tail.
  531. status = eScpiParserStatus_need_data; // warning: no closing quote found, need more data
  532. }
  533. // reset error code/status
  534. pObj->error = SCPI_ERROR_SUCCESS;
  535. pObj->pErrorMsg = NULL;
  536. break;
  537. }
  538. else // 0 != pObj->parseArbitraryBlock.LengthDigits
  539. {
  540. // the next @LengthDigits characters must be interpretated as a
  541. // ... part of the length entity of the header.
  542. // Nothing to do right here, see "if( 0 != pObj->parseArbitraryBlock.LengthDigits )"
  543. }
  544. }
  545. }
  546. else // 0 != pObj->parseArbitraryBlock.LengthDigits
  547. {
  548. // the next @LengthDigits characters must be interpretated as a
  549. // ... part of the length entity of the header.
  550. // check if the current character is a digit:
  551. if( ! scpi_isdigit( character ) )
  552. {
  553. pObj->error = SCPI_ERROR_INVALID_BLOCKDATA;
  554. pObj->pErrorMsg = SCPI_ERROR_INVALID_BLOCKDATA_MSG;
  555. // nope, it is a fiasco
  556. status = eScpiParserStatus_failed; // error: invalid data block header
  557. }
  558. else
  559. {
  560. pObj->parseArbitraryBlock.LengthDigits--; // decrease a number of digits
  561. // To get the length of the block it is required to process the
  562. // ... length-entity of the header. On each cycle the taken character
  563. // ... is a remainder of devision to 10, so it is required to
  564. // ... multiply the @Length by 10 and the remainder.
  565. pObj->parseArbitraryBlock.Length = pObj->parseArbitraryBlock.Length * 10 + (size_t)(character - '0');
  566. // As soon as all the digits are "swallowed":
  567. if( 0 == pObj->parseArbitraryBlock.LengthDigits )
  568. {
  569. // check if the length-entity is greater than zero
  570. if( 0 < pObj->parseArbitraryBlock.Length )
  571. {
  572. // Set the "data" indicator: all the @Length next characters
  573. // are the part of the payload.
  574. pObj->parseArbitraryBlock.DataStarted = true;
  575. }
  576. else
  577. {
  578. pObj->type = eScpiEntityTypeArbitraryBlock; // assign the entity type
  579. // Dummy block detected: the @Length is actually zero
  580. // The arbitrary data block has zero-length payload.
  581. // Calculate the whole block size in the buffer:
  582. // LENGTH = @str - @head
  583. // Assigning the @tail pointer to indicate the block length:
  584. pObj->tail = pObj->str; // update the tail pointer
  585. status = eScpiParserStatus_success; // The block was successfully recognized.
  586. // reset error code/status
  587. pObj->error = SCPI_ERROR_SUCCESS;
  588. pObj->pErrorMsg = NULL;
  589. break;
  590. }
  591. }
  592. }
  593. }
  594. }
  595. else
  596. {
  597. // Swallow each byte until all the @Length bytes is counted
  598. if( 0 < pObj->parseArbitraryBlock.Length )
  599. {
  600. // It is still a payload...
  601. pObj->parseArbitraryBlock.Length--;
  602. }
  603. // As soon as all the bytes of the block's payload are processed:
  604. if( 0 == pObj->parseArbitraryBlock.Length )
  605. {
  606. pObj->type = eScpiEntityTypeArbitraryBlock; // assign the entity type
  607. // the entity size in the buffer
  608. // LENGTH = @str - @head
  609. // Assign the @tail pointer to indicate the entity length:
  610. pObj->tail = pObj->str; // update the tail pointer
  611. status = eScpiParserStatus_success; // The block was successfully recognized.
  612. // reset error code/status
  613. pObj->error = SCPI_ERROR_SUCCESS;
  614. pObj->pErrorMsg = NULL;
  615. break;
  616. }
  617. }
  618. }
  619. }
  620. }
  621. return status;
  622. }
  623. // =================================================================================================================
  624. // @parseString
  625. // "Parser-function", parses a SCPI string (STRING PROGRAM DATA)
  626. // References:
  627. // "<STRING PROGRAM DATA> 7.7.5", [1]
  628. // "7.7.5.2 Encoding Syntax", [1]
  629. // Parameters:
  630. // @xObj - parser context, must be prepared by @prepareParserContext
  631. // ... before each call. If it is needed to continue the processing, the
  632. // ... @bKeepContext parameter duing the call @prepareParserContext must
  633. // ... be set to 'true', and otherwise, to restart parsing the parameter
  634. // ... must be set to 'false'.
  635. // Returns:
  636. // eScpiParserStatus_failed - error, can not parse data;
  637. // eScpiParserStatus_success - success, the string has been successfully parsed;
  638. // eScpiParserStatus_need_data - warning, the string can not be parsed due to
  639. // there no enough data to process.
  640. eScpiParserStatus_t parseString( xParseEntry_t * xObj )
  641. {
  642. sParseEntry_t * pObj = (sParseEntry_t*)xObj;
  643. eScpiParserStatus_t status = eScpiParserStatus_invalid;
  644. if( NULL != pObj && NULL != pObj->str && 0 != ((ptrdiff_t)pObj->tail - (ptrdiff_t)pObj->head)
  645. && (0 < ((ptrdiff_t)pObj->tail - (ptrdiff_t)pObj->str)) )
  646. {
  647. status = eScpiParserStatus_failed; // forward status set: the default 'eScpiParserStatus_invalid' shall
  648. // ... not be returned due to the context will be changed.
  649. // if the context has been cleaned up
  650. if( ! pObj->bContextKept )
  651. {
  652. // initialize private context variables
  653. pObj->parseString.QuoteOpened = false;
  654. pObj->parseString.LastCharacter = '\0'; // initialize the @LastCharacter to the any character, but not
  655. // ... single nor double quote.
  656. }
  657. // walk til the end of the buffer
  658. while( pObj->str != pObj->tail )
  659. {
  660. char character = *(pObj->str++);
  661. if( ! pObj->parseString.QuoteOpened )
  662. {
  663. if( '\"' == character || '\'' == character ) // !: double/signle quoted string
  664. {
  665. pObj->parseString.QuoteSignature = character;
  666. pObj->parseString.QuoteOpened = true;
  667. continue; // avoid of remembering current character in @LastCharacter
  668. }
  669. else // !: non-quote character
  670. {
  671. pObj->error = SCPI_ERROR_INVALID_STRINGDATA;
  672. pObj->pErrorMsg = SCPI_ERROR_INVALID_STRINGDATA_MSG;
  673. status = eScpiParserStatus_failed; // error: the data does not start from a quote
  674. break;
  675. }
  676. }
  677. else
  678. {
  679. // Analyze the character with 1-character delay to detect quote-escaping:
  680. if( pObj->parseString.QuoteSignature == pObj->parseString.LastCharacter ) // maybe, it is a nesting quoting (escaping):
  681. {
  682. // Need to analyze a couple of characters to ensure, if current character is a
  683. // ... closing quote character, or an ecaping one.
  684. if( pObj->parseString.QuoteSignature != character ) // check current character:
  685. {
  686. pObj->type = eScpiEntityTypeString; // assign the entity type
  687. pObj->parseString.QuoteOpened = false; // last character was a closing quote
  688. // calculate the string length:
  689. // LENGTH = @str - @head - 1
  690. // Assign the @tail pointer to indicate the entity length:
  691. pObj->tail = pObj->str - 1; // update the tail pointer
  692. status = eScpiParserStatus_success; // The string was successfully recognized.
  693. // reset error code/status
  694. pObj->error = SCPI_ERROR_SUCCESS;
  695. pObj->pErrorMsg = NULL;
  696. break;
  697. }
  698. else
  699. {
  700. pObj->parseString.LastCharacter = '\0'; // initialize the @LastCharacter to the any character, but not
  701. // ... single nor double quote.
  702. continue; // avoid of remembering current character in @LastCharacter
  703. }
  704. }
  705. }
  706. pObj->parseString.LastCharacter = character;
  707. }
  708. // Check if is there an opend quote:
  709. if( pObj->parseString.QuoteOpened )
  710. {
  711. if( pObj->bEndMessage )
  712. {
  713. pObj->error = SCPI_ERROR_INVALID_STRINGDATA;
  714. pObj->pErrorMsg = SCPI_ERROR_INVALID_STRINGDATA_MSG;
  715. status = eScpiParserStatus_failed; // error: last quote had not been closed
  716. }
  717. else
  718. {
  719. (void)pObj->type; // do not assign the entity type, it is not identified yet.
  720. status = eScpiParserStatus_need_data; // warning: no closing quote found, need more data
  721. }
  722. }
  723. }
  724. return status;
  725. }
  726. // =================================================================================================================
  727. // @parseProgramMenmonic
  728. // "Parser-function", parses a SCPI Program Mnemonic (<program mnemonic>)
  729. // References:
  730. // "<program mnemonic>", "7.6.1.2 Encoding Syntax", [1]
  731. // Parameters:
  732. // @xObj - parser context, must be prepared by @prepareParserContext
  733. // ... before each call. If it is needed to continue the processing, the
  734. // ... @bKeepContext parameter duing the call @prepareParserContext must
  735. // ... be set to 'true', and otherwise, to restart parsing the parameter
  736. // ... must be set to 'false'.
  737. // Returns:
  738. // eScpiParserStatus_failed - error, can not parse data;
  739. // eScpiParserStatus_success - success, the string has been successfully parsed;
  740. // eScpiParserStatus_need_data - warning, the string can not be parsed due to
  741. // there no enough data to process.
  742. eScpiParserStatus_t parseProgramMenmonic( xParseEntry_t * xObj )
  743. {
  744. sParseEntry_t * pObj = (sParseEntry_t*)xObj;
  745. eScpiParserStatus_t status = eScpiParserStatus_invalid;
  746. if( NULL != pObj && NULL != pObj->str && 0 != ((ptrdiff_t)pObj->tail - (ptrdiff_t)pObj->head)
  747. && (0 < ((ptrdiff_t)pObj->tail - (ptrdiff_t)pObj->str)) )
  748. {
  749. status = eScpiParserStatus_failed; // forward status set: the default 'eScpiParserStatus_invalid' shall
  750. // ... not be returned due to the context will be changed.
  751. // if the context has been cleaned up
  752. if( ! pObj->bContextKept )
  753. {
  754. // initialize private context variables
  755. pObj->parseProgramMnemonic.FirstAlpha = false; // reset the start-character indicator
  756. pObj->parseProgramMnemonic.CharsNumber = 0; // reset the number of processed characters
  757. }
  758. // walk til the end of the buffer
  759. while( pObj->str != pObj->tail )
  760. {
  761. char character = *(pObj->str++);
  762. // If the start-character have not found yet:
  763. if( ! pObj->parseProgramMnemonic.FirstAlpha )
  764. {
  765. // the first character must be the upper/lower case
  766. // ... ALPHA character, 7.6.1.2 Encoding Syntax, [1]
  767. if( scpi_isalpha( character ) )
  768. {
  769. pObj->parseProgramMnemonic.FirstAlpha = true; // ok, the first character found
  770. pObj->parseProgramMnemonic.CharsNumber = 1; // 1 character has been processed
  771. }
  772. else
  773. {
  774. pObj->error = SCPI_ERROR_INVALID_CHARACTER;
  775. pObj->pErrorMsg = SCPI_ERROR_INVALID_CHARACTER_MSG;
  776. #if 0
  777. if( scpi_iscmdsep( character ) || scpi_commonhdrind( character ) )
  778. {
  779. // conditionally valid character.
  780. // must be analyzed by caller
  781. }
  782. #endif
  783. // indicate invalid character
  784. pObj->head = (pObj->str - 1); // update head to identify the character
  785. pObj->tail = pObj->str;
  786. status = eScpiParserStatus_failed; // the first character does not match
  787. // ... to the rule, error.
  788. // avoid of "break" operator here due to it is required to skip all the
  789. // ... code section right after the while-cycle.
  790. goto L_parseProgramMenmonic_EXIT;
  791. }
  792. }
  793. else
  794. {
  795. #if 1
  796. // check the length of already processed characters:
  797. if( pObj->parseProgramMnemonic.CharsNumber >= 12 ) // 7.6.1.4.1 Length, [1]
  798. {
  799. pObj->error = SCPI_ERROR_MNEMONIC_TOOLONG;
  800. pObj->pErrorMsg = SCPI_ERROR_MNEMONIC_TOOLONG_MSG;
  801. status = eScpiParserStatus_failed; // error: maximum mnemoic length exceeded
  802. // avoid of "break" operator here due to it is required to skip all the
  803. // ... code section right after the while-cycle.
  804. goto L_parseProgramMenmonic_EXIT;
  805. }
  806. #endif
  807. // check the character:
  808. if( scpi_isalnum( character ) || ( '_' == character ) )
  809. {
  810. pObj->parseProgramMnemonic.CharsNumber++; // increase number of characters
  811. }
  812. else
  813. {
  814. // Since this character does not match to the rule,
  815. // ... it is required to roll back the iterator @str
  816. // ... due to it have been incremented illegally for
  817. // ... for this condition.
  818. pObj->str--; // roll back the iterator
  819. break;
  820. }
  821. }
  822. } // while(...)
  823. // check if the whole buffer has been processed without errors:
  824. if( pObj->str >= pObj->tail )
  825. {
  826. // yes, no separation element found, maybe, it is not complete entity.
  827. // calculate the entity length:
  828. // LENGTH = WHOLE_BUFFER_LENGTH
  829. // To indicate the entity length as a whole buffer length,
  830. // ... do not change the @tail pointer since it refers to
  831. // ... the end of the buffer.
  832. (void)pObj->type; // do not assign the entity type, it is not identified yet.
  833. // check if the end-of-message indicator is set:
  834. if( pObj->bEndMessage )
  835. {
  836. pObj->error = SCPI_ERROR_COMMAND_ERROR;
  837. pObj->pErrorMsg = SCPI_ERROR_COMMAND_ERROR_MSG;
  838. // no more data available: error
  839. status = eScpiParserStatus_failed;
  840. }
  841. else
  842. {
  843. // need more data to analyze:
  844. status = eScpiParserStatus_need_data;
  845. }
  846. }
  847. else
  848. // check if at least one matching character found:
  849. if( pObj->parseProgramMnemonic.FirstAlpha )
  850. {
  851. // assign the entity type
  852. pObj->type = eScpiEntityTypeProgramMnemonic;
  853. // calculate the entity length:
  854. // LENGTH = @str - @head
  855. // Assign the @tail pointer to indicate the entity length:
  856. pObj->tail = pObj->str; // assign the @tail pointer to the current iterator
  857. status = eScpiParserStatus_success; // success: program mnemonic has been found
  858. // reset error code/status
  859. pObj->error = SCPI_ERROR_SUCCESS;
  860. pObj->pErrorMsg = NULL;
  861. }
  862. else
  863. {
  864. pObj->error = SCPI_ERROR_COMMAND_ERROR;
  865. pObj->pErrorMsg = SCPI_ERROR_COMMAND_ERROR_MSG;
  866. status = eScpiParserStatus_failed; // error: no matching characters found
  867. }
  868. }
  869. L_parseProgramMenmonic_EXIT:
  870. return status;
  871. }
  872. // =================================================================================================================
  873. // @parseCharacter
  874. // "Parser-function", parses a SCPI character (CHARACTER PROGRAM DATA)
  875. // References:
  876. // "7.7.1 <CHARACTER PROGRAM DATA>", [1]
  877. // "7.7.1.2 Encoding Syntax", [1]
  878. // Parameters:
  879. // @xObj - parser context, must be prepared by @prepareParserContext
  880. // ... before each call. If it is needed to continue the processing, the
  881. // ... @bKeepContext parameter duing the call @prepareParserContext must
  882. // ... be set to 'true', and otherwise, to restart parsing the parameter
  883. // ... must be set to 'false'.
  884. // Returns:
  885. // eScpiParserStatus_failed - error, can not parse data;
  886. // eScpiParserStatus_success - success, the string has been successfully parsed;
  887. // eScpiParserStatus_need_data - warning, the string can not be parsed due to
  888. // there no enough data to process.
  889. eScpiParserStatus_t parseCharacter( xParseEntry_t * xObj )
  890. {
  891. sParseEntry_t * pObj = (sParseEntry_t*)xObj;
  892. // In accordance with "7.7.1 <CHARACTER PROGRAM DATA>", [1], the character
  893. // ... entity shall be parsed the same way as program mnemonic entity.
  894. eScpiParserStatus_t status = parseProgramMenmonic( xObj );
  895. if( eScpiParserStatus_success == status )
  896. {
  897. // override the entity type:
  898. pObj->type = eScpiEntityTypeCharacter;
  899. }
  900. return status;
  901. }
  902. // =================================================================================================================
  903. // @parseArray
  904. // "Parser-function", parses a SCPI array (ARRAY PROGRAM DATA)
  905. // References:
  906. // "7.7.1 <CHARACTER PROGRAM DATA>", [1]
  907. // "7.7.1.2 Encoding Syntax", [1]
  908. // Parameters:
  909. // @xObj - parser context, must be prepared by @prepareParserContext
  910. // ... before each call. If it is needed to continue the processing, the
  911. // ... @bKeepContext parameter duing the call @prepareParserContext must
  912. // ... be set to 'true', and otherwise, to restart parsing the parameter
  913. // ... must be set to 'false'.
  914. // Returns:
  915. // eScpiParserStatus_failed - error, can not parse data;
  916. // eScpiParserStatus_success - success, the string has been successfully parsed;
  917. // eScpiParserStatus_need_data - warning, the string can not be parsed due to
  918. // there no enough data to process.
  919. eScpiParserStatus_t parseArrayNumber( xParseEntry_t * xObj )
  920. {
  921. sParseEntry_t * pObj = (sParseEntry_t*)xObj;
  922. // In accordance with "7.7.1 <CHARACTER PROGRAM DATA>", [1], the character
  923. // ... entity shall be parsed the same way as program mnemonic entity.
  924. eScpiParserStatus_t status = parseProgramMenmonic( xObj );
  925. if( eScpiParserStatus_success == status )
  926. {
  927. // override the entity type:
  928. pObj->type = eScpiEntityTypeCharacter;
  929. }
  930. return status;
  931. }
  932. // =================================================================================================================
  933. // @parseCommandProgramHeader
  934. // "Parser-function", parses a SCPI Command/Query Program Header (<COMMAND PROGRAM HEADER>, <QUERY PROGRMA HEADER>)
  935. // References:
  936. // "7.6.1 <COMMAND PROGPRAM HEADER>", [1]
  937. // "7.6.1.2 Encoding Syntax", [1]
  938. // "7.6.2 <COMMAND PROGPRAM HEADER>", [1]
  939. // "7.6.2.2 Encoding Syntax", [1]
  940. // Parameters:
  941. // @xObj - parser context, must be prepared by @prepareParserContext
  942. // ... before each call. If it is needed to continue the processing, the
  943. // ... @bKeepContext parameter duing the call @prepareParserContext must
  944. // ... be set to 'true', and otherwise, to restart parsing the parameter
  945. // ... must be set to 'false'.
  946. // Returns:
  947. // eScpiParserStatus_failed - error, can not parse data;
  948. // eScpiParserStatus_success - success, the string has been successfully parsed;
  949. // eScpiParserStatus_need_data - warning, the string can not be parsed due to
  950. // ... there no enough data to process.
  951. // Notes: All white spaces before the found command program header is skipped,
  952. // ... the @pxEntityBegin parameter during @getParsedEntityDetails call will refer
  953. // ... to the command program header beginning.
  954. eScpiParserStatus_t parseCommandProgramHeader( xParseEntry_t * xObj )
  955. {
  956. sParseEntry_t * pObj = (sParseEntry_t*)xObj;
  957. eScpiParserStatus_t status = eScpiParserStatus_invalid;
  958. if( NULL != pObj && NULL != pObj->str && 0 != ((ptrdiff_t)pObj->tail - (ptrdiff_t)pObj->head)
  959. && (0 < ((ptrdiff_t)pObj->tail - (ptrdiff_t)pObj->str)) )
  960. {
  961. status = eScpiParserStatus_need_data; // forward status set: the default 'eScpiParserStatus_invalid' shall
  962. // ... not be returned due to the context will be changed.
  963. // if the context has been cleaned up
  964. if( ! pObj->bContextKept )
  965. {
  966. // initialize private context variables
  967. (void)pObj->parseCommandProgramHeader.FirstAlpha; // do not touch
  968. (void)pObj->parseCommandProgramHeader.CharsNumber; // do not touch
  969. pObj->parseCommandProgramHeader._tail = NULL; // it will be initialzed later
  970. pObj->parseCommandProgramHeader.LastCallContinue = false;
  971. pObj->parseCommandProgramHeader.LastCallSuccess = false;
  972. pObj->parseCommandProgramHeader.CommonForm = false;
  973. pObj->parseCommandProgramHeader.LastCharacter = ' '; // Rule 1: initialize the @LastCharacter to the
  974. // ... any character, but not a semicolon character.
  975. // Rule 2: initialize the @LastCharacter to the
  976. // ... white character to indicate that no non-white
  977. // ... character has been faced
  978. }
  979. // walk til the end of the buffer
  980. while( pObj->str != pObj->tail )
  981. {
  982. char character = *(pObj->str); // DO NOT INCREMENT ITERATOR
  983. // skip white characters:
  984. if( scpi_iswhite( character ) )
  985. {
  986. // In accordance with "7.6.1.2 <COMMAND PROGPRAM HEADER>":
  987. // Check if the last character is a white character:
  988. if( scpi_iswhite( pObj->parseCommandProgramHeader.LastCharacter ) )
  989. {
  990. // yes, thus no Non-white character faced yet -> any amount
  991. // ... of white-characters can be skipped in the beginning.
  992. pObj->parseCommandProgramHeader.LastCharacter = character;
  993. pObj->str++; // skip white character
  994. pObj->head = pObj->str; // update @head to skip white-spaces
  995. continue;
  996. }
  997. if( scpi_ismsgsep( pObj->parseCommandProgramHeader.LastCharacter ) )
  998. {
  999. // this white character can be skipped because the previous was a
  1000. // command separator
  1001. pObj->parseCommandProgramHeader.LastCharacter = character;
  1002. pObj->str++; // skip separator
  1003. pObj->head = pObj->str; // update @head to skip separator
  1004. continue;
  1005. }
  1006. }
  1007. if( scpi_ismsgsep( character ) )
  1008. {
  1009. if( scpi_ismsgsep( pObj->parseCommandProgramHeader.LastCharacter )
  1010. || scpi_iswhite( pObj->parseCommandProgramHeader.LastCharacter ) )
  1011. {
  1012. pObj->parseCommandProgramHeader.LastCharacter = character;
  1013. pObj->str++; // skip separator character
  1014. pObj->head = pObj->str; // update @head to skip character
  1015. continue;
  1016. }
  1017. }
  1018. // In accordance with "7.6.1 <COMMAND PROGPRAM HEADER>", [1], the command
  1019. // ... program mnemonic entity shall be parsed the similar way as program
  1020. // ... mnemonic entity. Thus this implementation uses @parseProgramMenmonic
  1021. // ... as a part.
  1022. //
  1023. // Need to preserve the @tail pointer to recover it after the call:
  1024. pObj->parseCommandProgramHeader._tail = pObj->tail; // preserve @tail
  1025. // Need to preserve the @head pointer to recover it after the call:
  1026. pObj->parseCommandProgramHeader._head = pObj->head; // preserve @head
  1027. // Nested @parseProgramMenmonic call uses it's own context needed to be kept
  1028. // between calls. If the last call failed, the context required to be reset.
  1029. // Reset @bContextKept indicator if the last call failed:
  1030. pObj->bContextKept = pObj->parseCommandProgramHeader.LastCallContinue;
  1031. // It is required to pass the character to the @parseProgramMenmonic() even
  1032. // ... it is a separator character to terminate @parseProgramMenmonic's context.
  1033. // Check if the @parseProgramMenmonic() have enough data to recognize entity:
  1034. if( eScpiParserStatus_need_data == (status = parseProgramMenmonic( xObj ), status ) )
  1035. {
  1036. // The processed chunk is a part of program mnemonic.
  1037. // Not enough data for complete recognition.
  1038. // The length of incomleted entity is a whole buffer size,
  1039. // ... it is need to assign the @tail to indicate the entity length.
  1040. // In this case the assigning the @tail is useless due to it is
  1041. // ... already refers to the end of the buffer (@parseProgramMenmonic
  1042. // ... call hasn't change it).
  1043. (void)pObj->tail;
  1044. // keep the @status code
  1045. (void)status;
  1046. // Remember the last chacracter processed.
  1047. // It is required to retrieve the last character by referring @str iterator.
  1048. // @str has been incremented since the last @parseProgramMenmonic call.
  1049. pObj->parseCommandProgramHeader.LastCharacter = *(pObj->str - 1);
  1050. // set the last-call indicator.
  1051. // It helps to determine if at least one call has been performed without error
  1052. pObj->parseCommandProgramHeader.LastCallContinue = true; // set last-call indicator
  1053. // reset error code/status
  1054. pObj->error = SCPI_ERROR_SUCCESS;
  1055. pObj->pErrorMsg = NULL;
  1056. break;
  1057. }
  1058. else if( eScpiParserStatus_success == status )
  1059. {
  1060. // Last call of @parseProgramMenmonic found progrma mnemonic as a part of command program header
  1061. // The @str iterator refers to the next character after the last mnemonic character
  1062. // reset the last-call indicator.
  1063. // It helps to determine if it is requried to reset the context before call.
  1064. // As soon as a command mnemonic found, it is requried to restart the context
  1065. pObj->parseCommandProgramHeader.LastCallContinue = false; // reset last-call indicator
  1066. // Remember that at least last call was succeeded,
  1067. // ... this fact will be helpful if the call returns 'eScpiParserStatus_faliled':
  1068. pObj->parseCommandProgramHeader.LastCallSuccess = true; // set last-call success indicator
  1069. // @str iterator already refers to the next character to continue processing.
  1070. (void)pObj->str;
  1071. // Check if the end of the buffer is reached:
  1072. if( pObj->parseCommandProgramHeader._tail <= pObj->str )
  1073. {
  1074. // Yes, the end of the buffer is reached
  1075. // Set 'eScpiParserStatus_success' status
  1076. (void)status; // @status already is eScpiParserStatus_success
  1077. // reset error code/status
  1078. pObj->error = SCPI_ERROR_SUCCESS;
  1079. pObj->pErrorMsg = NULL;
  1080. break;
  1081. }
  1082. // The entity had been recognized successfully, but it is not end of the buffer.
  1083. // Recover @tail before the next call:
  1084. pObj->tail = pObj->parseCommandProgramHeader._tail;
  1085. // Remember the last chacracter processed.
  1086. // It is required to retrieve the last character by referring @str iterator.
  1087. // @str has been incremented since the last @parseProgramMenmonic call.
  1088. pObj->parseCommandProgramHeader.LastCharacter = *(pObj->str - 1);
  1089. }
  1090. else // eScpiParserStatus_failed (the code 'eScpiParserStatus_invalid' is not considered as possible)
  1091. {
  1092. // Need to restore the @tail pointer to recover it after the unsuccessful call:
  1093. pObj->tail = pObj->parseCommandProgramHeader._tail; // restore @tail
  1094. // Need to restore the @head pointer to recover it after the unsuccessful call:
  1095. pObj->head = pObj->parseCommandProgramHeader._head; // restore @head
  1096. // The @parseProgramMenmonic call failed, maybe it is a separator?
  1097. if( scpi_iscmdsep( character ) ) // character ':'
  1098. {
  1099. // Check if the command program header is expected in common form:
  1100. // In common form the program header can not be split up by semicolons.
  1101. if( ! pObj->parseCommandProgramHeader.CommonForm )
  1102. {
  1103. // Compound command program header detected.
  1104. // Yes, it is a program mnemonic separator
  1105. // Check if this separator is the only separator by checking last character:
  1106. if( scpi_iscmdsep( pObj->parseCommandProgramHeader.LastCharacter ) ) // character ':'
  1107. {
  1108. // Last character is a colon-character too.
  1109. // error: double separator detected
  1110. status = eScpiParserStatus_failed;
  1111. pObj->error = SCPI_ERROR_INVSEP_ERROR;
  1112. pObj->pErrorMsg = SCPI_ERROR_INVSEP_ERROR_MSG;
  1113. break;
  1114. }
  1115. else
  1116. {
  1117. // reset error code/status
  1118. pObj->error = SCPI_ERROR_SUCCESS;
  1119. pObj->pErrorMsg = NULL;
  1120. // Last character is not a colon-character (maybe, there was no last character at all).
  1121. // Ok, it is a single program mnemonic separator.
  1122. // Let's remember this character and continue processing.
  1123. // @str is already incremented.
  1124. pObj->parseCommandProgramHeader.LastCharacter = character; // remember the last processed character
  1125. // warning: need more data to determine if this character is a leading
  1126. // ... semicolon or not.
  1127. status = eScpiParserStatus_need_data;
  1128. }
  1129. }
  1130. else
  1131. {
  1132. // Error: common command program header can not contain semicolons
  1133. status = eScpiParserStatus_failed; // error
  1134. pObj->error = SCPI_ERROR_COMMAND_HEADER_SEP;
  1135. pObj->pErrorMsg = SCPI_ERROR_COMMAND_HEADER_SEP_MSG;
  1136. break;
  1137. }
  1138. }
  1139. else // ':' !== character
  1140. if( scpi_commonhdrind( character ) ) // check for leading '*' ("common query program header", 7.6.1.2 Encoding Syntax, [1])
  1141. {
  1142. // Since the command program header can not contain white-character as described in 7.6 [1],
  1143. // ... if the @LastCharacter is a white-character then this character is the first header character
  1144. // processed. The Asterisk character in the beginnning of the command program header means that this
  1145. // ... is a common command program header. Let's check it:
  1146. if( scpi_iswhite(pObj->parseCommandProgramHeader.LastCharacter)
  1147. || scpi_ismsgsep(pObj->parseCommandProgramHeader.LastCharacter) )
  1148. {
  1149. // reset error code/status
  1150. pObj->error = SCPI_ERROR_SUCCESS;
  1151. pObj->pErrorMsg = NULL;
  1152. // Let's remember this character and continue processing.
  1153. pObj->parseCommandProgramHeader.LastCharacter = character; // remember the last processed character
  1154. // Set the common form indicator
  1155. pObj->parseCommandProgramHeader.CommonForm = true; // common program header is expected
  1156. // warning: need more data to determine if this character is a legal Asterisk character
  1157. // ... in the beginning of the command program header.
  1158. status = eScpiParserStatus_need_data;
  1159. }
  1160. else
  1161. {
  1162. // Asterisk character detected inside the command program header (not in the beginning);
  1163. // Asterisk character is illeagal inside the command program header.
  1164. status = eScpiParserStatus_failed;
  1165. pObj->error = SCPI_ERROR_COMMAND_HEADER_SEP; // missing separator?
  1166. pObj->pErrorMsg = SCPI_ERROR_COMMAND_HEADER_SEP_MSG;
  1167. break;
  1168. }
  1169. }
  1170. else // '*' !== character
  1171. if( scpi_queryind( character ) ) // check for ending '?' ("common query program header", 7.6.2.2 Encoding Syntax, [1])
  1172. {
  1173. // Since the command program header can not contain null-character as described in 7.6 [1],
  1174. // ... if the @LastCharacter is '\0' then this character is the first character processed.
  1175. // If the question mark character is faced in the end of the entity, it is allowed (7.6.2, [1]).
  1176. // If the @LastCharacter is not null and the @LastCallSuccess is true, it seems to be ending of
  1177. // ... the entity, but it's required to analyze next character.
  1178. if( '\0' != pObj->parseCommandProgramHeader.LastCharacter
  1179. &&
  1180. (!scpi_queryind(pObj->parseCommandProgramHeader.LastCharacter))
  1181. &&
  1182. (!scpi_iscmdsep(pObj->parseCommandProgramHeader.LastCharacter))
  1183. &&
  1184. pObj->parseCommandProgramHeader.LastCallSuccess )
  1185. {
  1186. // reset error code/status
  1187. pObj->error = SCPI_ERROR_SUCCESS;
  1188. pObj->pErrorMsg = NULL;
  1189. // need to continue analyzing to determine if this character is legal.
  1190. status = eScpiParserStatus_need_data;
  1191. // Let's remember this character and continue processing.
  1192. pObj->parseCommandProgramHeader.LastCharacter = character; // remember the last processed character
  1193. }
  1194. else
  1195. {
  1196. // error: illegal character faced
  1197. status = eScpiParserStatus_failed;
  1198. pObj->head = (pObj->str - 1); // update head to identify the character
  1199. pObj->tail = pObj->str;
  1200. pObj->error = SCPI_ERROR_INVALID_CHARACTER;
  1201. pObj->pErrorMsg = SCPI_ERROR_INVALID_CHARACTER_MSG;
  1202. break;
  1203. }
  1204. }
  1205. else // '?' !== character
  1206. // 7.4 Separator Functional Elements", [1]
  1207. // -check for terminating character '\n' (7.5 <PROGRAM MESSAGE TERMINATOR>, [1])
  1208. // -check for white character
  1209. // -check for program message unit separator (7.4.1 <PROGRAM MESSAGE UNIT SEPARATOR>, [1])
  1210. if( scpi_isnl( character ) || scpi_iswhite( character ) || scpi_ismsgsep( character ) )
  1211. {
  1212. // it is not a program mnemonic separator => it is a message terminator
  1213. // Check if the last @parseProgramMenmonic was successeded?
  1214. // Check if the last processed character is not a colon character?
  1215. if( pObj->parseCommandProgramHeader.LastCallSuccess && ( !scpi_iscmdsep(pObj->parseCommandProgramHeader.LastCharacter)) )
  1216. {
  1217. // yes, the last program mnemonic has been found successfully.
  1218. // Calculate the entity length:
  1219. // LENGTH = @str - 1; // minus one due to the last call have incremented it
  1220. pObj->tail = pObj->str - 1;
  1221. // It is end of command program header.
  1222. status = eScpiParserStatus_success;
  1223. // reset error code/status
  1224. pObj->error = SCPI_ERROR_SUCCESS;
  1225. pObj->pErrorMsg = NULL;
  1226. }
  1227. else
  1228. {
  1229. pObj->tail = pObj->str - 1; // store the @tail pointer to help the caller to identify the NL-character
  1230. pObj->error = SCPI_ERROR_COMMAND_HEADER; // no header?
  1231. pObj->pErrorMsg = SCPI_ERROR_COMMAND_HEADER_MSG;
  1232. status = eScpiParserStatus_failed;
  1233. }
  1234. break;
  1235. }
  1236. else // '\n' !== character, undefined character
  1237. {
  1238. // store the @tail pointer to help the caller to identify the invalid character
  1239. pObj->tail = pObj->str;
  1240. if( pObj->parseCommandProgramHeader.LastCallSuccess && (pObj->error != SCPI_ERROR_SUCCESS) )
  1241. {
  1242. pObj->head = (pObj->str - 1);
  1243. pObj->error = SCPI_ERROR_INVALID_CHARACTER;
  1244. pObj->pErrorMsg = SCPI_ERROR_INVALID_CHARACTER_MSG;
  1245. }
  1246. status = eScpiParserStatus_failed;
  1247. break;
  1248. }
  1249. }
  1250. } // while(...)
  1251. if( eScpiParserStatus_success == status )
  1252. {
  1253. // override the entity type:
  1254. // If the last processed character is question mark character,
  1255. // ... the entity type is the "query program header"
  1256. if( scpi_queryind( pObj->parseCommandProgramHeader.LastCharacter ) )
  1257. {
  1258. // Check for "Common form":
  1259. if( pObj->parseCommandProgramHeader.CommonForm )
  1260. {
  1261. pObj->type = eScpiEntityTypeCmnQueryProgHdr; // Common form
  1262. }
  1263. else
  1264. {
  1265. pObj->type = eScpiEntityTypeCmpQueryProgHdr; // Simple or Compound form
  1266. }
  1267. }
  1268. else // else: the "command program header"
  1269. {
  1270. // Check for "Common form":
  1271. if( pObj->parseCommandProgramHeader.CommonForm )
  1272. {
  1273. pObj->type = eScpiEntityTypeCmnCommandProgHdr; // Common form
  1274. }
  1275. else
  1276. {
  1277. pObj->type = eScpiEntityTypeCmpCommandProgHdr; // Simple or Compound form
  1278. }
  1279. }
  1280. }
  1281. else if( eScpiParserStatus_need_data == status ) // if it is requried more data to analyze:
  1282. {
  1283. // check if the end-of-message indicator is set:
  1284. if( pObj->bEndMessage )
  1285. {
  1286. pObj->error = SCPI_ERROR_COMMAND_HEADER; // no header, imcomplete string?
  1287. pObj->pErrorMsg = SCPI_ERROR_COMMAND_HEADER_MSG;
  1288. // no more data available: error
  1289. status = eScpiParserStatus_failed;
  1290. }
  1291. }
  1292. }
  1293. // L_parseCommandProgramHeader_EXIT:
  1294. return status;
  1295. }
  1296. // =================================================================================================================
  1297. // @parseDemicalNumber
  1298. // "Parser-function", parses a SCPI Demical Numeric Program Data ()
  1299. // References:
  1300. // "7.7.2 <DECIMAL NUMERIC PROGRAM DATA>", [1]
  1301. // "7.7.2.2 Encoding Syntax", [1]
  1302. // Parameters:
  1303. // @xObj - parser context, must be prepared by @prepareParserContext
  1304. // ... before each call. If it is needed to continue the processing, the
  1305. // ... @bKeepContext parameter duing the call @prepareParserContext must
  1306. // ... be set to 'true', and otherwise, to restart parsing the parameter
  1307. // ... must be set to 'false'.
  1308. // Returns:
  1309. // eScpiParserStatus_failed - error, can not parse data;
  1310. // eScpiParserStatus_success - success, the string has been successfully parsed;
  1311. // eScpiParserStatus_need_data - warning, the string can not be parsed due to
  1312. // there no enough data to process.
  1313. static eScpiParserStatus_t parseDemicalNumber( xParseEntry_t * xObj )
  1314. {
  1315. sParseEntry_t * pObj = (sParseEntry_t*)xObj;
  1316. eScpiParserStatus_t status = eScpiParserStatus_invalid;
  1317. if( NULL != pObj && NULL != pObj->str && 0 != ((ptrdiff_t)pObj->tail - (ptrdiff_t)pObj->head)
  1318. && (0 < ((ptrdiff_t)pObj->tail - (ptrdiff_t)pObj->str)) )
  1319. {
  1320. status = eScpiParserStatus_need_data; // forward status set: the default 'eScpiParserStatus_invalid' shall
  1321. // ... not be returned due to the context will be changed.
  1322. // if the context has been cleaned up
  1323. if( ! pObj->bContextKept )
  1324. {
  1325. // "7.7.2.2 Encoding Syntax", [1]:
  1326. pObj->parseDemicalNumber.parseStage = false; // false - search for mantissa
  1327. pObj->parseDemicalNumber.signAllowed = true; // sign - allowed
  1328. pObj->parseDemicalNumber.dotAllowed = true; // dot - allowed
  1329. pObj->parseDemicalNumber.digitAllowed = true; // digit - allowed
  1330. pObj->parseDemicalNumber.whiteAllowed = false; // white - disallowed
  1331. pObj->parseDemicalNumber.expAllowed = false; // exp - disallowed
  1332. pObj->parseDemicalNumber.termAllowed = false; // msgsep - disallowed
  1333. pObj->parseDemicalNumber.dotFound = false; // reset dot indicator
  1334. pObj->parseDemicalNumber.manitssaOk = false; // reset mantissa indicator
  1335. pObj->parseDemicalNumber.exponentOk = false; // reset exponent indicator
  1336. }
  1337. // walk til the end of the buffer or message separator is faced
  1338. while( pObj->str != pObj->tail )
  1339. {
  1340. char character = *(pObj->str++);
  1341. // Check if the sign-character is allowed in current state:
  1342. if( pObj->parseDemicalNumber.signAllowed )
  1343. {
  1344. // Check if the character is a sign-character:
  1345. if( '-' == character || '+' == character )
  1346. {
  1347. if( ! pObj->parseDemicalNumber.parseStage ) // search for mantissa
  1348. {
  1349. // Only digits and dot-character are allowed after the sign-character for mantissa
  1350. // since the sign-character faced, it shall be disallowed.
  1351. pObj->parseDemicalNumber.signAllowed = false;
  1352. // since the sign-character faced, white-character shall be disallowed.
  1353. pObj->parseDemicalNumber.whiteAllowed = false;
  1354. // since the sign-character faced, dot-character shall be allowed.
  1355. pObj->parseDemicalNumber.dotAllowed = true;
  1356. // since the sign-character faced, exp-character shall be disallowed.
  1357. pObj->parseDemicalNumber.expAllowed = false;
  1358. // * since the sign-character faced, digits shall be allowed.
  1359. pObj->parseDemicalNumber.digitAllowed = true;
  1360. // * since the sign-character faced, message unit separator is disallowed
  1361. pObj->parseDemicalNumber.termAllowed = false;
  1362. }
  1363. else // search for exponent
  1364. {
  1365. // Only digits are allowed after the sign-character for exponent
  1366. // since the sign-character faced, it shall be disallowed.
  1367. pObj->parseDemicalNumber.signAllowed = false;
  1368. // since the sign-character faced, white-character shall be disallowed.
  1369. pObj->parseDemicalNumber.whiteAllowed = false;
  1370. // since the sign-character faced, dot-character shall be disallowed.
  1371. pObj->parseDemicalNumber.dotAllowed = false;
  1372. // since the sign-character faced, exp-character shall be disallowed.
  1373. pObj->parseDemicalNumber.expAllowed = false;
  1374. // * since the sign-character faced, digits shall be allowed.
  1375. pObj->parseDemicalNumber.digitAllowed = true;
  1376. // * since the sign-character faced, message unit separator is disallowed
  1377. pObj->parseDemicalNumber.termAllowed = false;
  1378. }
  1379. // skip character
  1380. continue;
  1381. }
  1382. }
  1383. // Check if the dot-character is allowed in current state:
  1384. if( pObj->parseDemicalNumber.dotAllowed )
  1385. {
  1386. // Check if the character is a dot-character:
  1387. if( '.' == character )
  1388. {
  1389. // dot faced
  1390. pObj->parseDemicalNumber.dotFound = true;
  1391. if( ! pObj->parseDemicalNumber.parseStage ) // search for mantissa
  1392. {
  1393. // Digits, white-character and exp-character are allowed after the dot-character in mantissa.
  1394. // since the dot-character faced, it shall be disallowed.
  1395. pObj->parseDemicalNumber.dotAllowed = false;
  1396. // since the dot-character faced, sign-character shall be disallowed.
  1397. pObj->parseDemicalNumber.signAllowed = false;
  1398. // since the dot-character faced, white-character shall be allowed,
  1399. // ... (switching to exponent mode)
  1400. pObj->parseDemicalNumber.whiteAllowed = true;
  1401. // * since the dot-character faced, digits shall be allowed.
  1402. pObj->parseDemicalNumber.digitAllowed = true;
  1403. // since the dot-character faced, exp-character shall be allowed,
  1404. // ... (switching to exponent mode)
  1405. pObj->parseDemicalNumber.expAllowed = true;
  1406. // * since the dot-character faced, message unit separator is disallowed
  1407. pObj->parseDemicalNumber.termAllowed = false;
  1408. }
  1409. // skip character
  1410. continue;
  1411. }
  1412. }
  1413. // Check if the digit-character is allowed in current state:
  1414. if( pObj->parseDemicalNumber.digitAllowed )
  1415. {
  1416. // Check if the character is a digit-character:
  1417. if( scpi_isdigit( character ) )
  1418. {
  1419. if( ! pObj->parseDemicalNumber.parseStage ) // search for mantissa
  1420. {
  1421. // Digits, dot-character, exp-character and white-character are allowed after digit in mantissa
  1422. // since the digit-character faced, sign-character shall be disallowed.
  1423. pObj->parseDemicalNumber.signAllowed = false;
  1424. // since the digit-character faced, white-character shall be allowed,
  1425. // ... (switching to exponent mode)
  1426. pObj->parseDemicalNumber.whiteAllowed = true;
  1427. // since the digit-character faced, exp-character shall be allowed,
  1428. // ... (switching to exponent mode)
  1429. pObj->parseDemicalNumber.expAllowed = true;
  1430. // * since the digit-character faced, dot-character shall be allowed.
  1431. #if 0 // false-positive: "-3.1.e-1", second dot shall not be allowed after digit '1'
  1432. pObj->parseDemicalNumber.dotAllowed = true;
  1433. #else
  1434. (void)pObj->parseDemicalNumber.dotAllowed; // already enabled since start
  1435. #endif
  1436. // * since the digit-character faced, message unit separator is allowed
  1437. pObj->parseDemicalNumber.termAllowed = true;
  1438. // since at least one digit faced in mantissa, the mantissa is OK
  1439. pObj->parseDemicalNumber.manitssaOk = true;
  1440. (void)pObj->parseDemicalNumber.digitAllowed; // already enabled
  1441. }
  1442. else // 2 - search for exponent;
  1443. {
  1444. // Only digits are allowed after digit in exponent or message separator
  1445. // since the digit-character faced, sign-character shall be disallowed.
  1446. pObj->parseDemicalNumber.signAllowed = false;
  1447. // since the digit-character faced, dot-character shall be disallowed.
  1448. pObj->parseDemicalNumber.dotAllowed = false;
  1449. // since the digit-character faced, white-character shall be disallowed (note*).
  1450. pObj->parseDemicalNumber.whiteAllowed = false;
  1451. // since the digit-character faced, sign-character shall be disallowed.
  1452. pObj->parseDemicalNumber.signAllowed = false;
  1453. // since the digit-character faced in exponent, message unit separator is allowed
  1454. // Note*: @termAllowed covers a white-characters too only if @exponentOk is set.
  1455. pObj->parseDemicalNumber.termAllowed = true;
  1456. (void)pObj->parseDemicalNumber.digitAllowed; // already enabled
  1457. // As soon as first digit faced in exponent, the exponent can be
  1458. // ... considered as a valid exponent.
  1459. pObj->parseDemicalNumber.exponentOk = true;
  1460. }
  1461. // skip character
  1462. continue;
  1463. }
  1464. }
  1465. // Check if the message-unit-separator-character is allowed in current state:
  1466. if( pObj->parseDemicalNumber.termAllowed )
  1467. {
  1468. // Check if the character is:
  1469. // - a message-unit separator;
  1470. // - a data-unit separator;
  1471. // - a message-unit terminator;
  1472. // - a white character (a kind of terminating character only if no entities expected more);
  1473. if( scpi_isdatasep( character ) || scpi_ismsgsep( character ) || scpi_isnl( character ) || (scpi_iswhite( character ) && pObj->parseDemicalNumber.exponentOk) )
  1474. {
  1475. if( ! pObj->parseDemicalNumber.parseStage ) // search for mantissa
  1476. {
  1477. // Facing the separator-character in mantissa means end of number:
  1478. pObj->str--; // roll back the iterator to previous mantissa character
  1479. break; // interrupt processing
  1480. }
  1481. else // search for exponent
  1482. {
  1483. // In general the separator-character in exponent is allowable only if @exponentOk is true.
  1484. // But if message separator found and no valid exponend anounced, it is required to consider
  1485. // found entity as a number containing only mantissa.
  1486. // only if no exponent is found
  1487. if( ! pObj->parseDemicalNumber.exponentOk )
  1488. // switch stage back to mantissa mode
  1489. pObj->parseDemicalNumber.parseStage = false; // guarantees the number is valid
  1490. { // discard all found white spaces after mantissa:
  1491. pObj->str--; // roll back iterator to the separator character
  1492. pObj->str--; // roll back iterator to the character before the separator
  1493. while( scpi_iswhite( *pObj->str ) ) pObj->str--; // discard white characters found
  1494. pObj->str++; // skip the last mantissa character
  1495. }
  1496. break; // interrupt processing
  1497. }
  1498. }
  1499. }
  1500. // Check if the white-character is allowed in current state:
  1501. if( pObj->parseDemicalNumber.whiteAllowed )
  1502. {
  1503. // Check if the character is a white-character:
  1504. if( scpi_iswhite( character ) )
  1505. {
  1506. if( ! pObj->parseDemicalNumber.parseStage ) // search for mantissa
  1507. {
  1508. // Due to facing the white-character in mantissa means switching to
  1509. // ... searching-for-exponent mode, only exp-character is allowed:
  1510. // since the white-character faced, the stage shall be changed:
  1511. pObj->parseDemicalNumber.parseStage = true; // search for exponent
  1512. // since the white-character faced, exp-character shall be allowed.
  1513. pObj->parseDemicalNumber.expAllowed = true;
  1514. // since the white-character faced, dot-character shall be disallowed.
  1515. pObj->parseDemicalNumber.dotAllowed = false;
  1516. // since the white-character faced, sign-character shall be disallowed.
  1517. pObj->parseDemicalNumber.signAllowed = false;
  1518. // since the white-character faced, digits shall be disallowed.
  1519. pObj->parseDemicalNumber.digitAllowed = false;
  1520. // since the mode is being changed, the mantissa has been processed
  1521. (void)pObj->parseDemicalNumber.manitssaOk; // already set to true in digit-handler
  1522. (void)pObj->parseDemicalNumber.whiteAllowed; // already enabled
  1523. }
  1524. else // search for exponent
  1525. {
  1526. // Only digits, sign-character and exp-character are allowed in exponent
  1527. // since the white-character faced, sign-character shall be allowed in exponent,
  1528. // ... but white-character may be faced either before and after exp-character, thus
  1529. // ... do not change the allowing of sign-character here:
  1530. (void)pObj->parseDemicalNumber.signAllowed; // do not touch
  1531. // * since the white-character faced, dot-character shall be disallowed.
  1532. pObj->parseDemicalNumber.dotAllowed = false; // already disabled ealier
  1533. // Due to white-character may be faced either before and after the exp-character,
  1534. // ... the digit-character allowing shall not be changed here, but it shall be
  1535. // ... enabled on either the dot-character and digit-character handler.
  1536. (void)pObj->parseDemicalNumber.digitAllowed; // do not touch
  1537. // Due to white-character may be faced either before and after the exp-character,
  1538. // ... the exp-character allowing shall not be changed here, but it shall be
  1539. // ... enabled on either the dot-character and digit-character handler.
  1540. (void)pObj->parseDemicalNumber.expAllowed; // do not touch
  1541. (void)pObj->parseDemicalNumber.whiteAllowed; // already enabled
  1542. }
  1543. // skip character
  1544. continue;
  1545. }
  1546. }
  1547. // Check if the white-character is allowed in current state:
  1548. if( pObj->parseDemicalNumber.expAllowed )
  1549. {
  1550. // Check if the character is a digit-character:
  1551. if( 'E' == character || 'e' == character )
  1552. {
  1553. if( ! pObj->parseDemicalNumber.parseStage ) // search for mantissa
  1554. {
  1555. // Since the exp-character is faced in mantissa the mode shall be
  1556. // ... changed to exponent mode. Thus only digits, white-character
  1557. // ... and sign-character are allowed.
  1558. // since the exp-character faced, the stage shall be changed:
  1559. pObj->parseDemicalNumber.parseStage = true; // search for exponent
  1560. // since the exp-character faced, it shall be disallowed.
  1561. pObj->parseDemicalNumber.expAllowed = false;
  1562. // since the exp-character faced, sign-character shall be allowed.
  1563. pObj->parseDemicalNumber.signAllowed = true;
  1564. // since the exp-character faced, dot-character shall be disallowed.
  1565. pObj->parseDemicalNumber.dotAllowed = false;
  1566. // * since the exp-character faced, white-character shall be allowed.
  1567. pObj->parseDemicalNumber.whiteAllowed = true;
  1568. // * since the exp-character faced, digits shall be allowed.
  1569. pObj->parseDemicalNumber.digitAllowed = true;
  1570. }
  1571. else // possibly if white-character faced after mantissa:
  1572. {
  1573. // since the exp-character faced, the stage shall be changed:
  1574. (void)pObj->parseDemicalNumber.parseStage; // already set
  1575. // since the exp-character faced, it shall be disallowed.
  1576. pObj->parseDemicalNumber.expAllowed = false;
  1577. // since the exp-character faced, dot-character shall be disallowed.
  1578. pObj->parseDemicalNumber.dotAllowed = false;
  1579. // since the exp-character faced, white-character shall be allowed,
  1580. // ... but this situation is only possible if white-characters has
  1581. // ... been already faced before exp-character, thus white-character
  1582. // ... is already allowed.
  1583. (void)pObj->parseDemicalNumber.whiteAllowed; // already set
  1584. // * since the exp-character faced, digits shall be allowed.
  1585. pObj->parseDemicalNumber.digitAllowed = true;
  1586. // since the exp-character faced, sign-character shall be allowed.
  1587. pObj->parseDemicalNumber.signAllowed = true;
  1588. }
  1589. // * since the exp-character faced, terminating characters are disallowed
  1590. pObj->parseDemicalNumber.termAllowed = false;
  1591. // skip character
  1592. continue;
  1593. }
  1594. }
  1595. // unsupported character faced: break with 'failed' status
  1596. pObj->error = SCPI_ERROR_INVALID_NUMERIC_CHAR;
  1597. pObj->pErrorMsg = SCPI_ERROR_INVALID_NUMERIC_CHAR_MSG;
  1598. pObj->head = pObj->str - 1;
  1599. pObj->tail = pObj->str;
  1600. status = eScpiParserStatus_failed;
  1601. break;
  1602. }
  1603. // If no unsupported character faced (just buffer ran out): eScpiParserStatus_need_data and
  1604. // ... end-of-message indicator is set, or:
  1605. // ... if unsupported character faced: eScpiParserStatus_failed
  1606. if( eScpiParserStatus_failed == status || ( eScpiParserStatus_need_data == status && pObj->bEndMessage ) )
  1607. {
  1608. if( eScpiParserStatus_failed == status )
  1609. {
  1610. pObj->str--; // roll back the iterator to previous supported character
  1611. }
  1612. // easy... Let's check the mantissa:
  1613. if( pObj->parseDemicalNumber.manitssaOk )
  1614. {
  1615. // the mantissa is ok, then let's check
  1616. // ... the exponent part only if the parse stage is search-for-exponent:
  1617. if( pObj->parseDemicalNumber.parseStage )
  1618. {
  1619. // exponent expected:
  1620. // Let's check if valid exponent found:
  1621. if( pObj->parseDemicalNumber.exponentOk )
  1622. {
  1623. // valid exponent found!
  1624. // Calculate the entity length:
  1625. // LENGTH = @str - @head
  1626. // Set the length:
  1627. pObj->tail = pObj->str;
  1628. pObj->type = eScpiEntityTypeDemicalExpNumber; // set the entity type: mantissa+exponent
  1629. status = eScpiParserStatus_success;
  1630. // reset error code/status
  1631. pObj->error = SCPI_ERROR_SUCCESS;
  1632. pObj->pErrorMsg = NULL;
  1633. } else { /* no valid mantissa found -> error */ }
  1634. }
  1635. else
  1636. {
  1637. // no exponent even announced, just end of mantissa!
  1638. // Calculate the entity length:
  1639. // LENGTH = @str - @head
  1640. // Set the length:
  1641. pObj->tail = pObj->str;
  1642. if( pObj->parseDemicalNumber.dotFound )
  1643. pObj->type = eScpiEntityTypeDemicalFloatNumber; // set the entity type: only mantissa
  1644. else
  1645. pObj->type = eScpiEntityTypeDemicalIntegerNumber; // set the entity type: integer (mantissa)
  1646. // reset error code/status
  1647. pObj->error = SCPI_ERROR_SUCCESS;
  1648. pObj->pErrorMsg = NULL;
  1649. status = eScpiParserStatus_success;
  1650. }
  1651. } else { /* no valid mantissa found -> error */ }
  1652. } else { /* Impossible: eScpiParserStatus_success; Useless: eScpiParserStatus_invalid */ }
  1653. }
  1654. return status;
  1655. }
  1656. // =================================================================================================================
  1657. static eScpiParserStatus_t parseNonDemicalNumber( xParseEntry_t * xObj );
  1658. static eScpiParserStatus_t parseDemicalNumber( xParseEntry_t * xObj );
  1659. // @parseNumber_fParsers[]
  1660. // Array of parser functions to be used in attempts to parse numeric value by @parseNumber routine
  1661. eScpiParserStatus_t (* const parseNumber_fParsers[2])( xParseEntry_t * xObj ) =
  1662. {
  1663. parseNonDemicalNumber , parseDemicalNumber // order is important!
  1664. };
  1665. // =================================================================================================================
  1666. // @parseNumber
  1667. // "Parser-function", parses a SCPI Demical Numeric Program Data and SCPI Hexademical/Octal/Binary Numeric Program Data
  1668. // References:
  1669. // "7.7.2 <DECIMAL NUMERIC PROGRAM DATA>", [1]
  1670. // "7.7.2.2 Encoding Syntax", [1]
  1671. // "7.7.4 <NONDECIMAL NUMERIC PROGRAM DATA>", [1]
  1672. // "7.7.4.2 Encoding Syntax", [1]
  1673. // Parameters:
  1674. // @xObj - parser context, must be prepared by @prepareParserContext
  1675. // ... before each call. If it is needed to continue the processing, the
  1676. // ... @bKeepContext parameter duing the call @prepareParserContext must
  1677. // ... be set to 'true', and otherwise, to restart parsing the parameter
  1678. // ... must be set to 'false'.
  1679. // Returns:
  1680. // eScpiParserStatus_failed - error, can not parse data;
  1681. // eScpiParserStatus_success - success, the string has been successfully parsed;
  1682. // eScpiParserStatus_need_data - warning, the string can not be parsed due to
  1683. // there no enough data to process.
  1684. eScpiParserStatus_t parseNumber( xParseEntry_t * xObj )
  1685. {
  1686. eScpiParserStatus_t status = eScpiParserStatus_failed;
  1687. sParseEntry_t * pObj = (sParseEntry_t*)xObj;
  1688. int32_t nParserIdx = 0;
  1689. if( ! pObj->bContextKept )
  1690. {
  1691. // backup parser entry point (save iterators)
  1692. parserBackupContext( pObj, &pObj->parseNumber_xSaveObj );
  1693. // backup parser index (reset index in the storage)
  1694. parserBackupContextValue( &pObj->parseNumber_xSaveObj, nParserIdx );
  1695. }
  1696. else
  1697. {
  1698. // restore parser index
  1699. parserRestoreContextValue( &pObj->parseNumber_xSaveObj, &nParserIdx );
  1700. }
  1701. L_parseNumber_DO:
  1702. // try parse
  1703. switch( status = parseNumber_fParsers[nParserIdx]( xObj ), status )
  1704. {
  1705. // parser need more data to decide
  1706. case eScpiParserStatus_need_data:
  1707. {
  1708. // backup parser index (update index in the storage)
  1709. parserBackupContextValue( &pObj->parseNumber_xSaveObj, nParserIdx );
  1710. // return original status to the caller
  1711. (void)status;
  1712. }
  1713. break;
  1714. case eScpiParserStatus_failed:
  1715. {
  1716. // parser failed
  1717. // Check for special condition:
  1718. if( parseNumber_fParsers[nParserIdx] == parseNonDemicalNumber )
  1719. if( pObj->parseNonDemicalNumber.foundNumericChar )
  1720. {
  1721. // @parseNonDemicalNumber failed and the numeric system character was found.
  1722. // Do not try to parse using @parseDemicalNumber to keep error code and condition.
  1723. // Parsing using @parseDemicalNumber is impossible any way ('#' character)
  1724. break;
  1725. }
  1726. // is there another parser?
  1727. if( (1+nParserIdx) < cellsof(parseNumber_fParsers) )
  1728. {
  1729. // yep, try this
  1730. nParserIdx++;
  1731. // backup parser context again (update index in the storage)
  1732. parserBackupContextValue( &pObj->parseNumber_xSaveObj, nParserIdx );
  1733. // restore parser iterators
  1734. parserRestoreContext( pObj, &pObj->parseNumber_xSaveObj );
  1735. // reset error status code
  1736. pObj->error = SCPI_ERROR_SUCCESS;
  1737. // try another parser
  1738. goto L_parseNumber_DO;
  1739. }
  1740. // return original status to the caller
  1741. (void)status;
  1742. }
  1743. break;
  1744. case eScpiParserStatus_success:
  1745. {
  1746. // succeeded
  1747. // return original status to the caller
  1748. (void)status;
  1749. }
  1750. break;
  1751. case eScpiParserStatus_invalid:
  1752. {
  1753. // wrong param
  1754. // return original status to the caller
  1755. (void)status;
  1756. }
  1757. break;
  1758. default:
  1759. // unexpected code
  1760. status = eScpiParserStatus_invalid;
  1761. my_assert( false );
  1762. }
  1763. return status;
  1764. }
  1765. // =================================================================================================================
  1766. // @parseNonDemicalNumber
  1767. // "Parser-function", parses a SCPI Hexademical/Octal/Binary Numeric Program Data ()
  1768. // References:
  1769. // "7.7.4 <NONDECIMAL NUMERIC PROGRAM DATA>", [1]
  1770. // "7.7.4.2 Encoding Syntax", [1]
  1771. // Parameters:
  1772. // @xObj - parser context, must be prepared by @prepareParserContext
  1773. // ... before each call. If it is needed to continue the processing, the
  1774. // ... @bKeepContext parameter duing the call @prepareParserContext must
  1775. // ... be set to 'true', and otherwise, to restart parsing the parameter
  1776. // ... must be set to 'false'.
  1777. // Returns:
  1778. // eScpiParserStatus_failed - error, can not parse data;
  1779. // eScpiParserStatus_success - success, the string has been successfully parsed;
  1780. // eScpiParserStatus_need_data - warning, the string can not be parsed due to
  1781. // there no enough data to process.
  1782. static eScpiParserStatus_t parseNonDemicalNumber( xParseEntry_t * xObj )
  1783. {
  1784. sParseEntry_t * pObj = (sParseEntry_t*)xObj;
  1785. eScpiParserStatus_t status = eScpiParserStatus_invalid;
  1786. if( NULL != pObj && NULL != pObj->str && 0 != ((ptrdiff_t)pObj->tail - (ptrdiff_t)pObj->head)
  1787. && (0 < ((ptrdiff_t)pObj->tail - (ptrdiff_t)pObj->str)) )
  1788. {
  1789. status = eScpiParserStatus_need_data; // forward status set: the default 'eScpiParserStatus_invalid' shall
  1790. // ... not be returned due to the context will be changed.
  1791. // if the context has been cleaned up
  1792. if( ! pObj->bContextKept )
  1793. {
  1794. pObj->parseNonDemicalNumber.allow0_1 = false;
  1795. pObj->parseNonDemicalNumber.allow0_7 = false;
  1796. pObj->parseNonDemicalNumber.allow0_f = false;
  1797. pObj->parseNonDemicalNumber.allowNumChar = false;
  1798. pObj->parseNonDemicalNumber.foundNumericChar = false;
  1799. pObj->parseNonDemicalNumber.digits = 0;
  1800. }
  1801. // walk til the end of the buffer or message separator is faced
  1802. while( pObj->str != pObj->tail && (!scpi_ismsgsep(*pObj->str)) )
  1803. {
  1804. char character = *(pObj->str++);
  1805. // check if binary character are allowed:
  1806. if( pObj->parseNonDemicalNumber.allow0_1 )
  1807. {
  1808. // if the character fits to the filter: digits 0..1
  1809. if( '0' == character || '1' == character )
  1810. {
  1811. pObj->parseNonDemicalNumber.digits++; // take the digit in account
  1812. // skip character
  1813. continue;
  1814. }
  1815. }
  1816. // check if octal character are allowed:
  1817. if( pObj->parseNonDemicalNumber.allow0_7 )
  1818. {
  1819. // if the character fits to the filter: digits 0..7
  1820. if( '0' <= character && '7' >= character )
  1821. {
  1822. pObj->parseNonDemicalNumber.digits++; // take the digit in account
  1823. // skip character
  1824. continue;
  1825. }
  1826. }
  1827. // check if octal character are allowed:
  1828. if( pObj->parseNonDemicalNumber.allow0_f )
  1829. {
  1830. // if the character fits to the filter: hex-chars
  1831. if( scpi_ishex( character) )
  1832. {
  1833. pObj->parseNonDemicalNumber.digits++; // take the digit in account
  1834. // skip character
  1835. continue;
  1836. }
  1837. }
  1838. // check if numeric system designator is allowed:
  1839. if( ! pObj->parseNonDemicalNumber.allowNumChar )
  1840. {
  1841. // not yet, thus the '#' is expected to detect the begining of the entity
  1842. // check if the '#' character is faced:
  1843. if( '#' == character )
  1844. {
  1845. // yes, the entity start is detected
  1846. pObj->parseNonDemicalNumber.allowNumChar = true;
  1847. // set indicator: numeric system character found
  1848. pObj->parseNonDemicalNumber.foundNumericChar = true; // pass to parseNumber
  1849. // skip character
  1850. continue;
  1851. }
  1852. status = eScpiParserStatus_failed; // invalid character faced
  1853. pObj->error = SCPI_ERROR_INVALID_NUMERIC_CHAR;
  1854. pObj->pErrorMsg = SCPI_ERROR_INVALID_NUMERIC_CHAR_MSG;
  1855. pObj->head = pObj->str - 1;
  1856. pObj->tail = pObj->str;
  1857. break;
  1858. }
  1859. else
  1860. {
  1861. // yes, check for the numeric system designator only if no the numeric
  1862. // ... system designator has been faced yet:
  1863. if( !pObj->parseNonDemicalNumber.allow0_1
  1864. &&
  1865. !pObj->parseNonDemicalNumber.allow0_7
  1866. &&
  1867. !pObj->parseNonDemicalNumber.allow0_f )
  1868. {
  1869. switch( character )
  1870. {
  1871. case 'B':
  1872. case 'b': pObj->parseNonDemicalNumber.allow0_1 = true; continue;
  1873. case 'Q':
  1874. case 'q': pObj->parseNonDemicalNumber.allow0_7 = true; continue;
  1875. case 'H':
  1876. case 'h': pObj->parseNonDemicalNumber.allow0_f = true; continue;
  1877. }
  1878. status = eScpiParserStatus_failed; // invalid character faced: unknown designator
  1879. pObj->error = SCPI_ERROR_NUMERIC_DATAERROR;
  1880. pObj->pErrorMsg = SCPI_ERROR_NUMERIC_DATAERROR_MSG;
  1881. pObj->head = pObj->str - 1;
  1882. pObj->tail = pObj->str;
  1883. break;
  1884. }
  1885. else
  1886. {
  1887. // If @character here is a digit or a letter, it is probably invalid character in the number
  1888. // Do not consider this character as an end of the number, instead of this it is better to generate an error.
  1889. if( 0 == pObj->parseNonDemicalNumber.digits || isdigit(character) || isalpha(character) )
  1890. {
  1891. // No one of @.allow0_* rules match, and no digits faced yet
  1892. status = eScpiParserStatus_failed; // invalid character faced:
  1893. pObj->error = SCPI_ERROR_INVALID_NUMERIC_CHAR;
  1894. pObj->pErrorMsg = SCPI_ERROR_INVALID_NUMERIC_CHAR_MSG;
  1895. pObj->head = pObj->str - 1;
  1896. pObj->tail = pObj->str;
  1897. break;
  1898. }
  1899. else
  1900. {
  1901. status = eScpiParserStatus_success; // the entity end found
  1902. // reset error code/status
  1903. pObj->error = SCPI_ERROR_SUCCESS;
  1904. pObj->pErrorMsg = NULL;
  1905. // roll back the iterator due to current character is invalid
  1906. pObj->str--;
  1907. // exit with success status
  1908. break;
  1909. }
  1910. }
  1911. }
  1912. }
  1913. // check the conditions:
  1914. // - if it is required more data and no more data is expected ( @bEndMessage is set)
  1915. // - if the success status set
  1916. if( (eScpiParserStatus_need_data == status && pObj->bEndMessage)
  1917. || (eScpiParserStatus_success == status ) )
  1918. {
  1919. // check if at least one numeric character found:
  1920. if( 0 < pObj->parseNonDemicalNumber.digits )
  1921. {
  1922. // set the entity length:
  1923. // LENGTH = @str - @head
  1924. pObj->tail = pObj->str; // set @tail to indicate the length
  1925. // set the entity type
  1926. if( pObj->parseNonDemicalNumber.allow0_1 )
  1927. {
  1928. pObj->type = eScpiEntityTypeBinNumber; // binary
  1929. pObj->head += 2; // skip '#B'
  1930. }
  1931. else if( pObj->parseNonDemicalNumber.allow0_7 )
  1932. {
  1933. pObj->type = eScpiEntityTypeOctNumber; // octal
  1934. pObj->head += 2; // skip '#Q'
  1935. }
  1936. else
  1937. {
  1938. pObj->type = eScpiEntityTypeHexNumber; // hexademical
  1939. pObj->head += 2; // skip '#H'
  1940. }
  1941. status = eScpiParserStatus_success;
  1942. }
  1943. else
  1944. {
  1945. status = eScpiParserStatus_failed; // error, no one numeric character found
  1946. pObj->error = SCPI_ERROR_NUMERIC_DATAERROR;
  1947. pObj->pErrorMsg = SCPI_ERROR_NUMERIC_DATAERROR_MSG;
  1948. }
  1949. }
  1950. }
  1951. return status;
  1952. }
  1953. // =================================================================================================================
  1954. // @parseProgramDataSeparator
  1955. // "Parser-function", parses a SCPI Program Data Separator
  1956. // References:
  1957. // "7.4 Separator Functional Elements", [1]
  1958. // "7.4.2.2 Encoding Syntax", [1]
  1959. // "7.5 <PROGRAM MESSAGE TERMINATOR>", [1]
  1960. // Parameters:
  1961. // @xObj - parser context, must be prepared by @prepareParserContext
  1962. // ... before each call. If it is needed to continue the processing, the
  1963. // ... @bKeepContext parameter duing the call @prepareParserContext must
  1964. // ... be set to 'true', and otherwise, to restart parsing the parameter
  1965. // ... must be set to 'false'.
  1966. // Returns:
  1967. // eScpiParserStatus_failed - error, can not parse data;
  1968. // eScpiParserStatus_success - success, the string has been successfully parsed;
  1969. // eScpiParserStatus_need_data - warning, the string can not be parsed due to
  1970. // there no enough data to process.
  1971. eScpiParserStatus_t parseProgramDataSeparator( xParseEntry_t * xObj )
  1972. {
  1973. sParseEntry_t * pObj = (sParseEntry_t*)xObj;
  1974. eScpiParserStatus_t status = eScpiParserStatus_invalid;
  1975. if( NULL != pObj && NULL != pObj->str && 0 != ((ptrdiff_t)pObj->tail - (ptrdiff_t)pObj->head)
  1976. && (0 < ((ptrdiff_t)pObj->tail - (ptrdiff_t)pObj->str)) )
  1977. {
  1978. status = eScpiParserStatus_need_data; // forward status set: the default 'eScpiParserStatus_invalid' shall
  1979. // ... not be returned due to the context will be changed.
  1980. // if the context has been cleaned up
  1981. if( ! pObj->bContextKept )
  1982. {
  1983. pObj->parseProgramDataSeparator.sepFound = false;
  1984. }
  1985. // walk til the end of the buffer
  1986. while( pObj->str != pObj->tail )
  1987. {
  1988. char character = *(pObj->str++);
  1989. // check for white-character
  1990. if( scpi_iswhite( character ) )
  1991. {
  1992. continue; // skip character
  1993. }
  1994. // check for NL-character
  1995. if( scpi_isnl( character ) )
  1996. {
  1997. pObj->parseProgramDataSeparator.sepFound = true;
  1998. pObj->type = eScpiEntityType_Service_MsgTerm;
  1999. status = eScpiParserStatus_success; // NL-character terminates Program Data entity
  2000. pObj->head = (pObj->str - 1);
  2001. pObj->tail = pObj->str;
  2002. // reset error code/status
  2003. pObj->error = SCPI_ERROR_SUCCESS;
  2004. pObj->pErrorMsg = NULL;
  2005. break;
  2006. }
  2007. // check if the data separator character is found
  2008. if( !pObj->parseProgramDataSeparator.sepFound )
  2009. {
  2010. // no, search for data separator character
  2011. if( scpi_isdatasep( character ) )
  2012. {
  2013. pObj->parseProgramDataSeparator.sepFound = true; // found
  2014. pObj->type = eScpiEntityType_Service_DataSep;
  2015. pObj->head = (pObj->str - 1);
  2016. pObj->tail = pObj->str;
  2017. status = eScpiParserStatus_success; // valid character
  2018. // reset error code/status
  2019. pObj->error = SCPI_ERROR_SUCCESS;
  2020. pObj->pErrorMsg = NULL;
  2021. break; // end of search
  2022. }
  2023. }
  2024. pObj->head = (pObj->str - 1);
  2025. pObj->tail = pObj->str;
  2026. status = eScpiParserStatus_failed; // unexpected character
  2027. pObj->error = SCPI_ERROR_INVSEP_ERROR;
  2028. pObj->pErrorMsg = SCPI_ERROR_INVSEP_ERROR_MSG;
  2029. break;
  2030. }
  2031. // check if it is required to receive more data to recognize:
  2032. if( eScpiParserStatus_need_data == status )
  2033. {
  2034. // check if this message is the last one?
  2035. if( pObj->bEndMessage )
  2036. {
  2037. // it is not required to the separator must be found
  2038. // ... the last message's character is reached.
  2039. // The end-of-message signal represents another data
  2040. // ... separator, so thus, the data separator is found.
  2041. status = eScpiParserStatus_success;
  2042. pObj->type = eScpiEntityType_Service_MsgTerm;
  2043. // reset error code/status
  2044. pObj->error = SCPI_ERROR_SUCCESS;
  2045. pObj->pErrorMsg = NULL;
  2046. }
  2047. // Note: if no unexpected character has been faced yet it means that
  2048. // ... the data separator still continues.
  2049. }
  2050. else
  2051. // check if an unexpected character faced:
  2052. if( eScpiParserStatus_failed == status )
  2053. {
  2054. // check if the separator character has been found?
  2055. if( pObj->parseProgramDataSeparator.sepFound )
  2056. {
  2057. status = eScpiParserStatus_success;
  2058. pObj->type = eScpiEntityType_Service_MsgTerm;
  2059. // reset error code/status
  2060. pObj->error = SCPI_ERROR_SUCCESS;
  2061. pObj->pErrorMsg = NULL;
  2062. }
  2063. // Note: if no separator character has been faced yet while an unexpected
  2064. // ... character already faced it means that the data separator not found.
  2065. }
  2066. }
  2067. return status;
  2068. }
  2069. // =================================================================================================================
  2070. // @parseProgramDataSeparator
  2071. // "Parser-function", parses a SCPI Program Header Separator
  2072. // References:
  2073. // "7.4.3 <PROGRAM HEADER SEPARATOR>, [1]
  2074. // "7.4.3.2 Encoding Syntax", [1]
  2075. // "7.5 <PROGRAM MESSAGE TERMINATOR>", [1]
  2076. // Parameters:
  2077. // @xObj - parser context, must be prepared by @prepareParserContext
  2078. // ... before each call. If it is needed to continue the processing, the
  2079. // ... @bKeepContext parameter duing the call @prepareParserContext must
  2080. // ... be set to 'true', and otherwise, to restart parsing the parameter
  2081. // ... must be set to 'false'.
  2082. // Returns:
  2083. // eScpiParserStatus_failed - error, can not parse data;
  2084. // eScpiParserStatus_success - success, the string has been successfully parsed;
  2085. // eScpiParserStatus_need_data - warning, the string can not be parsed due to
  2086. // there no enough data to process.
  2087. eScpiParserStatus_t parseProgramHeaderSeparator( xParseEntry_t * xObj )
  2088. {
  2089. sParseEntry_t * pObj = (sParseEntry_t*)xObj;
  2090. eScpiParserStatus_t status = eScpiParserStatus_invalid;
  2091. if( NULL != pObj && NULL != pObj->str && 0 != ((ptrdiff_t)pObj->tail - (ptrdiff_t)pObj->head)
  2092. && (0 < ((ptrdiff_t)pObj->tail - (ptrdiff_t)pObj->str)) )
  2093. {
  2094. status = eScpiParserStatus_need_data; // forward status set: the default 'eScpiParserStatus_invalid' shall
  2095. // ... not be returned due to the context will be changed.
  2096. // if the context has been cleaned up
  2097. if( ! pObj->bContextKept )
  2098. {
  2099. pObj->parseProgramHeaderSeparator.sepFound = false;
  2100. }
  2101. // walk til the end of the buffer
  2102. while( pObj->str != pObj->tail )
  2103. {
  2104. char character = *(pObj->str);
  2105. // check for white-character
  2106. if( scpi_iswhite( character ) )
  2107. {
  2108. if( ! pObj->parseProgramHeaderSeparator.sepFound )
  2109. {
  2110. pObj->head = pObj->str;
  2111. }
  2112. pObj->parseProgramHeaderSeparator.sepFound = true; // at least one white-character found
  2113. pObj->type = eScpiEntityType_Service_HdrSep;
  2114. pObj->str++; // skip character
  2115. continue; // skip character
  2116. }
  2117. // check for NL-character
  2118. if( scpi_isnl( character ) )
  2119. {
  2120. pObj->parseProgramHeaderSeparator.sepFound = true; // at least one white-character found
  2121. pObj->type = eScpiEntityType_Service_MsgTerm;
  2122. pObj->head = pObj->str - 1;
  2123. pObj->tail = pObj->str;
  2124. status = eScpiParserStatus_success; // NL character terminates the program header
  2125. // reset error code/status
  2126. pObj->error = SCPI_ERROR_SUCCESS;
  2127. pObj->pErrorMsg = NULL;
  2128. break;
  2129. }
  2130. // check if an unexpected character found
  2131. // check if at least one white-character found
  2132. if( pObj->parseProgramHeaderSeparator.sepFound )
  2133. {
  2134. // yes, it is success
  2135. // At least white character found thus the separator found too
  2136. status = eScpiParserStatus_success;
  2137. pObj->type = eScpiEntityType_Service_HdrSep;
  2138. (void)pObj->head;
  2139. pObj->tail = pObj->str;
  2140. // reset error code/status
  2141. pObj->error = SCPI_ERROR_SUCCESS;
  2142. pObj->pErrorMsg = NULL;
  2143. break;
  2144. }
  2145. pObj->head = (pObj->str - 1);
  2146. pObj->tail = pObj->str;
  2147. status = eScpiParserStatus_failed; // unexpected character found
  2148. pObj->error = SCPI_ERROR_INVSEP_ERROR;
  2149. pObj->pErrorMsg = SCPI_ERROR_INVSEP_ERROR_MSG;
  2150. break;
  2151. }
  2152. // check if there no enough data to detect the end of separator:
  2153. if( eScpiParserStatus_need_data == status )
  2154. {
  2155. // Note: AT LEAST ONE WHITE CHARACTER FOUND due to the @eScpiParserStatus_need_data status set
  2156. // check if this data portion is the last one:
  2157. if( pObj->bEndMessage )
  2158. {
  2159. // At least white character found thus the separator found too
  2160. status = eScpiParserStatus_success;
  2161. pObj->type = eScpiEntityType_Service_MsgTerm;
  2162. pObj->tail = pObj->str;
  2163. // reset error code/status
  2164. pObj->error = SCPI_ERROR_SUCCESS;
  2165. pObj->pErrorMsg = NULL;
  2166. }
  2167. }
  2168. }
  2169. return status;
  2170. }
  2171. // =================================================================================================================
  2172. // @parseProgramMessageSeparator
  2173. // "Parser-function", parses a SCPI Program Message Separator
  2174. // References:
  2175. // "7.4.1 <PROGRAM MESSAGE UNIT SEPARATOR>", [1]
  2176. // "7.4.1.2 Encoding Syntax", [1]
  2177. // "7.5 <PROGRAM MESSAGE TERMINATOR>", [1]
  2178. // Parameters:
  2179. // @xObj - parser context, must be prepared by @prepareParserContext
  2180. // ... before each call. If it is needed to continue the processing, the
  2181. // ... @bKeepContext parameter duing the call @prepareParserContext must
  2182. // ... be set to 'true', and otherwise, to restart parsing the parameter
  2183. // ... must be set to 'false'.
  2184. // Returns:
  2185. // eScpiParserStatus_failed - error, can not parse data;
  2186. // eScpiParserStatus_success - success, the string has been successfully parsed;
  2187. // eScpiParserStatus_need_data - warning, the string can not be parsed due to
  2188. // there no enough data to process.
  2189. eScpiParserStatus_t parseProgramMessageSeparator( xParseEntry_t * xObj )
  2190. {
  2191. sParseEntry_t * pObj = (sParseEntry_t*)xObj;
  2192. eScpiParserStatus_t status = eScpiParserStatus_invalid;
  2193. if( NULL != pObj && NULL != pObj->str && 0 != ((ptrdiff_t)pObj->tail - (ptrdiff_t)pObj->head)
  2194. && (0 < ((ptrdiff_t)pObj->tail - (ptrdiff_t)pObj->str)) )
  2195. {
  2196. status = eScpiParserStatus_need_data; // forward status set: the default 'eScpiParserStatus_invalid' shall
  2197. // ... not be returned due to the context will be changed.
  2198. // if the context has been cleaned up
  2199. if( ! pObj->bContextKept )
  2200. {
  2201. pObj->parseProgramMessageSeparator.sepFound = false;
  2202. }
  2203. // walk til the end of the buffer
  2204. while( pObj->str != pObj->tail )
  2205. {
  2206. char character = *(pObj->str++);
  2207. if( !pObj->parseProgramMessageSeparator.sepFound )
  2208. {
  2209. // check for white-character
  2210. if( scpi_iswhite( character ) )
  2211. {
  2212. pObj->tail = pObj->str;
  2213. continue; // skip character
  2214. }
  2215. // check for message-separator-character
  2216. if( scpi_ismsgsep( character ) )
  2217. {
  2218. pObj->parseProgramMessageSeparator.sepFound = true;
  2219. pObj->type = eScpiEntityType_Service_MsgSep;
  2220. pObj->head = pObj->str - 1;
  2221. pObj->tail = pObj->str;
  2222. break; // separator found
  2223. }
  2224. }
  2225. pObj->head = (pObj->str - 1);
  2226. pObj->tail = pObj->str;
  2227. status = eScpiParserStatus_failed; // unexpected character
  2228. pObj->error = SCPI_ERROR_INVSEP_ERROR;
  2229. pObj->pErrorMsg = SCPI_ERROR_INVSEP_ERROR_MSG;
  2230. break;
  2231. }
  2232. if( eScpiParserStatus_need_data == status )
  2233. {
  2234. if( pObj->parseProgramMessageSeparator.sepFound )
  2235. {
  2236. status = eScpiParserStatus_success; // separator found
  2237. pObj->type = eScpiEntityType_Service_MsgTerm;
  2238. // reset error code/status
  2239. pObj->error = SCPI_ERROR_SUCCESS;
  2240. pObj->pErrorMsg = NULL;
  2241. }
  2242. }
  2243. }
  2244. return status;
  2245. }
  2246. // =================================================================================================================
  2247. // @parseEndOfCommand
  2248. // "Parser-function", searches end of program command unit.
  2249. // References:
  2250. // "7.4.1 <PROGRAM MESSAGE UNIT SEPARATOR>", [1]
  2251. // "7.4.1.2 Encoding Syntax", [1]
  2252. // "7.5 <PROGRAM MESSAGE TERMINATOR>", [1]
  2253. // Parameters:
  2254. // @xObj - parser context, must be prepared by @prepareParserContext
  2255. // ... before each call. If it is needed to continue the processing, the
  2256. // ... @bKeepContext parameter duing the call @prepareParserContext must
  2257. // ... be set to 'true', and otherwise, to restart parsing the parameter
  2258. // ... must be set to 'false'.
  2259. // Returns:
  2260. // eScpiParserStatus_failed - error, can not parse data;
  2261. // eScpiParserStatus_success - success, the string has been successfully parsed;
  2262. // eScpiParserStatus_need_data - warning, the string can not be parsed due to
  2263. // there no enough data to process.
  2264. eScpiParserStatus_t parseEndOfCommand( xParseEntry_t * xObj )
  2265. {
  2266. sParseEntry_t * pObj = (sParseEntry_t*)xObj;
  2267. eScpiParserStatus_t status = eScpiParserStatus_invalid;
  2268. if( NULL != pObj && NULL != pObj->str && 0 != ((ptrdiff_t)pObj->tail - (ptrdiff_t)pObj->head)
  2269. && (0 < ((ptrdiff_t)pObj->tail - (ptrdiff_t)pObj->str)) )
  2270. {
  2271. status = eScpiParserStatus_need_data; // forward status set: the default 'eScpiParserStatus_invalid' shall
  2272. // ... not be returned due to the context will be changed.
  2273. // if the context has been cleaned up
  2274. if( ! pObj->bContextKept )
  2275. {
  2276. pObj->parseEndOfCommand.sepFound = false;
  2277. }
  2278. // walk til the end of the buffer
  2279. while( pObj->str != pObj->tail )
  2280. {
  2281. char character = *(pObj->str++);
  2282. if( !pObj->parseEndOfCommand.sepFound )
  2283. {
  2284. // check for white-character
  2285. if( scpi_iswhite( character ) )
  2286. {
  2287. continue; // skip character
  2288. }
  2289. // check for message-separator-character
  2290. if( scpi_ismsgsep( character ) )
  2291. {
  2292. pObj->parseEndOfCommand.sepFound = true;
  2293. pObj->head = (pObj->str - 1);
  2294. pObj->tail = pObj->str;
  2295. pObj->type = eScpiEntityType_Service_MsgSep;
  2296. break; // separator found
  2297. }
  2298. // check for message-terminator-character
  2299. if( scpi_isnl( character ) )
  2300. {
  2301. pObj->parseEndOfCommand.sepFound = true;
  2302. pObj->head = (pObj->str - 1);
  2303. pObj->tail = pObj->str;
  2304. pObj->type = eScpiEntityType_Service_MsgTerm;
  2305. break; // separator found
  2306. }
  2307. }
  2308. pObj->head = (pObj->str - 1);
  2309. pObj->tail = pObj->str;
  2310. status = eScpiParserStatus_failed; // unexpected character
  2311. pObj->error = SCPI_ERROR_INVSEP_ERROR;
  2312. pObj->pErrorMsg = SCPI_ERROR_INVSEP_ERROR_MSG;
  2313. break;
  2314. }
  2315. if( eScpiParserStatus_need_data == status )
  2316. {
  2317. if( pObj->parseEndOfCommand.sepFound )
  2318. {
  2319. status = eScpiParserStatus_success; // separator found
  2320. pObj->type = eScpiEntityType_Service_MsgTerm;
  2321. // reset error code/status
  2322. pObj->error = SCPI_ERROR_SUCCESS;
  2323. pObj->pErrorMsg = NULL;
  2324. }
  2325. }
  2326. }
  2327. return status;
  2328. }
  2329. // =================================================================================================================
  2330. // @parseWhiteSequence
  2331. // "Parser-function", searches for white characters sequence or NL-terminator
  2332. // References:
  2333. // "7.4.1.2 Encoding Syntax", [1]
  2334. // "7.5 <PROGRAM MESSAGE TERMINATOR>", [1]
  2335. // Parameters:
  2336. // @xObj - parser context, must be prepared by @prepareParserContext
  2337. // ... before each call. If it is needed to continue the processing, the
  2338. // ... @bKeepContext parameter duing the call @prepareParserContext must
  2339. // ... be set to 'true', and otherwise, to restart parsing the parameter
  2340. // ... must be set to 'false'.
  2341. // Note: this function does not modify @str iterator if no white character found
  2342. // Returns:
  2343. // eScpiParserStatus_failed - error, can not parse data;
  2344. // eScpiParserStatus_success - success, the string has been successfully parsed;
  2345. // eScpiParserStatus_need_data - warning, the string can not be parsed due to
  2346. // there no enough data to process.
  2347. eScpiParserStatus_t parseWhiteSequence( xParseEntry_t * xObj )
  2348. {
  2349. sParseEntry_t * pObj = (sParseEntry_t*)xObj;
  2350. eScpiParserStatus_t status = eScpiParserStatus_invalid;
  2351. if( NULL != pObj && NULL != pObj->str && 0 != ((ptrdiff_t)pObj->tail - (ptrdiff_t)pObj->head)
  2352. && (0 < ((ptrdiff_t)pObj->tail - (ptrdiff_t)pObj->str)) )
  2353. {
  2354. status = eScpiParserStatus_need_data; // forward status set: the default 'eScpiParserStatus_invalid' shall
  2355. // ... not be returned due to the context will be changed.
  2356. // if the context has been cleaned up
  2357. if( ! pObj->bContextKept )
  2358. {
  2359. pObj->parseWhiteSequence.whiteFound = false;
  2360. pObj->parseWhiteSequence.nlFound = false;
  2361. }
  2362. // walk til the end of the buffer
  2363. while( pObj->str != pObj->tail )
  2364. {
  2365. char character = *(pObj->str);
  2366. // check for white-character
  2367. if( scpi_iswhite( character ) )
  2368. {
  2369. pObj->parseWhiteSequence.whiteFound = true;
  2370. pObj->str++; // skip white character
  2371. (void)pObj->head; // keep @head pointer to point to the start of white-sequence
  2372. continue; // skip character
  2373. }
  2374. // check for white-character
  2375. if( scpi_isnl( character ) )
  2376. {
  2377. pObj->parseWhiteSequence.nlFound = true;
  2378. (void)pObj->str++; // skip NL-character
  2379. (void)pObj->head; // keep @head pointer to point to the start of white-sequence
  2380. break; // stop searching
  2381. }
  2382. if( !pObj->parseWhiteSequence.whiteFound )
  2383. {
  2384. pObj->head = (pObj->str - 1);
  2385. pObj->tail = pObj->str;
  2386. status = eScpiParserStatus_failed; // unexpected character
  2387. pObj->error = SCPI_ERROR_INVALID_CHARACTER;
  2388. pObj->pErrorMsg = SCPI_ERROR_INVALID_CHARACTER_MSG;
  2389. }
  2390. break; // non-white character found
  2391. }
  2392. if( eScpiParserStatus_need_data == status )
  2393. {
  2394. if( pObj->parseWhiteSequence.whiteFound )
  2395. {
  2396. if( pObj->bEndMessage )
  2397. {
  2398. // reset error code/status
  2399. pObj->error = SCPI_ERROR_SUCCESS;
  2400. pObj->pErrorMsg = NULL;
  2401. pObj->tail = pObj->str; // update the tail pointer
  2402. pObj->type = eScpiEntityType_Service_White;
  2403. status = eScpiParserStatus_success;
  2404. }
  2405. }
  2406. else
  2407. if( pObj->parseWhiteSequence.nlFound )
  2408. {
  2409. // reset error code/status
  2410. pObj->error = SCPI_ERROR_SUCCESS;
  2411. pObj->pErrorMsg = NULL;
  2412. pObj->tail = pObj->str; // update the tail pointer
  2413. pObj->type = eScpiEntityType_Service_MsgTerm;
  2414. status = eScpiParserStatus_success;
  2415. }
  2416. }
  2417. }
  2418. return status;
  2419. }
  2420. // =================================================================================================================
  2421. // @checkNotEndOfString
  2422. // "Parser-function", checks if the parser context not reached the end of string
  2423. // Parameters:
  2424. // @xObj - parser context, must be prepared by @prepareParserContext
  2425. // ... before each call. If it is needed to continue the processing, the
  2426. // ... @bKeepContext parameter duing the call @prepareParserContext must
  2427. // ... be set to 'true', and otherwise, to restart parsing the parameter
  2428. // ... must be set to 'false'.
  2429. // Returns:
  2430. // eScpiParserStatus_failed - error, end-of-string is reached
  2431. // eScpiParserStatus_success - success, end-of-string is not reached
  2432. // eScpiParserStatus_invalid - error, invalid parameters
  2433. eScpiParserStatus_t checkNotEndOfString( xParseEntry_t * xObj )
  2434. {
  2435. sParseEntry_t * pObj = (sParseEntry_t*)xObj;
  2436. eScpiParserStatus_t status = eScpiParserStatus_invalid;
  2437. if( NULL != pObj && NULL != pObj->str && 0 <= ((ptrdiff_t)pObj->tail - (ptrdiff_t)pObj->head)
  2438. && 0 <= ((ptrdiff_t)pObj->str - (ptrdiff_t)pObj->head) )
  2439. {
  2440. if( 0 < ((ptrdiff_t)pObj->tail - (ptrdiff_t)pObj->str) )
  2441. {
  2442. status = eScpiParserStatus_success;
  2443. }
  2444. else if( 0 == ((ptrdiff_t)pObj->tail - (ptrdiff_t)pObj->str) )
  2445. {
  2446. status = eScpiParserStatus_failed;
  2447. }
  2448. }
  2449. return status;
  2450. }
  2451. // =================================================================================================================
  2452. #pragma pack(push,1)
  2453. static const struct
  2454. {
  2455. eScpiParserStatus_t (*f)( xParseEntry_t * xObj );
  2456. uint8_t type;
  2457. }
  2458. parseArgumentsMap[] =
  2459. {
  2460. { parseCharacter, eScpiArg_Character },
  2461. { parseString, eScpiArg_String },
  2462. { parseArbitraryBlock, eScpiArg_Datablock },
  2463. { parseNumber, eScpiArg_Numeric },
  2464. { parseDemicalNumber, eScpiArg_Numeric_Demical },
  2465. { parseNonDemicalNumber, eScpiArg_Numeric_NonDemical },
  2466. { parseDemicalNumber , eScpiArg_Array_Decimal }
  2467. };
  2468. #pragma pack(pop)
  2469. // =================================================================================================================
  2470. // @parseArguments
  2471. // Common parser function to analyze and recognize command arguments using the
  2472. // template preassigned by @DECLARE_SCPI_ARGS macro.
  2473. // Parameters:
  2474. // @xObj - parser context, must be prepared by @prepareParserContext
  2475. // ... before each call. If it is needed to continue the processing, the
  2476. // ... @bKeepContext parameter duing the call @prepareParserContext must
  2477. // ... be set to 'true', and otherwise, to restart parsing the parameter
  2478. // ... must be set to 'false'.
  2479. // @argTokens - array of argument string tokens to store parameters iterators;
  2480. // @argTypes - array of argument types [eScpiArgType_t] to set the parameters parsing methods;
  2481. // @entityTypes - array of entity types [eScpiEntityType_t] to be written after successful call; optional;
  2482. // @argN - number of arguments (including optional and mandatory ones);
  2483. // @argOptionalN - number of optional only arguments;
  2484. // @pArgParsed - pointer to the variable to store the number of successfully
  2485. // ... parsed arguments, valid only if eScpiParserStatus_success status is returned,
  2486. // ... Otherwise, if the status is eScpiParserStatus_failed this value indicates
  2487. // ... the index of erroneous parameter. Can not be NULL.
  2488. // Returns:
  2489. // eScpiParserStatus_failed - error, can not parse data;
  2490. // eScpiParserStatus_invalid - error, invalid parameters;
  2491. // eScpiParserStatus_success - success, the string has been successfully parsed;
  2492. // eScpiParserStatus_need_data - warning, the string can not be parsed due to
  2493. // there no enough data to process.
  2494. eScpiParserStatus_t parseArguments( xParseEntry_t * xObj,
  2495. sStrToken_t * argTokens,
  2496. const uint8_t * argTypes,
  2497. uint8_t * entityTypes,
  2498. size_t argN,
  2499. size_t argOptionalN,
  2500. uint8_t * pArgParsed )
  2501. {
  2502. my_assert( xObj );
  2503. my_assert( argTokens );
  2504. my_assert( pArgParsed );
  2505. my_assert( argN >= argOptionalN );
  2506. my_assert( argTypes || argN == 0 );
  2507. sParseEntry_t * pObj = (sParseEntry_t*)xObj;
  2508. eScpiParserStatus_t status = eScpiParserStatus_invalid;
  2509. pObj->error = SCPI_ERROR_SYNTAX_ERROR;
  2510. pObj->pErrorMsg = SCPI_ERROR_SYNTAX_ERROR_PARAM_MSG;
  2511. // check parameters
  2512. if( NULL == xObj && NULL == argTokens && (NULL == argTypes && 0 != argN) && NULL == pArgParsed && (argN < argOptionalN) )
  2513. {
  2514. return eScpiParserStatus_invalid;
  2515. }
  2516. // special condition: no parameters are allowed
  2517. if( 0 == argN )
  2518. {
  2519. status = parseEndOfCommand( xObj );
  2520. if( status != eScpiParserStatus_success )
  2521. {
  2522. pObj->error = SCPI_ERROR_PARAMETER_NOTALLOWED;
  2523. pObj->pErrorMsg = SCPI_ERROR_PARAMETER_NOTALLOWED_MSG;
  2524. }
  2525. // no break, no return, 'for' will not be executed due to argN==0
  2526. }
  2527. bool parseBreak = false;
  2528. bool trailingDataSep = false;
  2529. size_t nArgs = 0;
  2530. for( size_t a = 0; (a < argN) && (!parseBreak); ++a )
  2531. {
  2532. *pArgParsed = a; // indicate current parameter index (failed case)
  2533. eScpiParserStatus_t (*fParser)( xParseEntry_t * xObj ) = NULL;
  2534. // search for parser routine by the specified argument type
  2535. for( size_t f = 0; f < sizeof(parseArgumentsMap)/sizeof(*parseArgumentsMap); ++f )
  2536. {
  2537. if( parseArgumentsMap[f].type == argTypes[a] )
  2538. {
  2539. // parser found
  2540. fParser = parseArgumentsMap[f].f;
  2541. break;
  2542. }
  2543. }
  2544. if( NULL == fParser )
  2545. {
  2546. // parser not found
  2547. status = eScpiParserStatus_failed;
  2548. break;
  2549. }
  2550. // keep tail pointer before searching for white characters
  2551. const char * _tail = pObj->tail;
  2552. // skip white characters
  2553. status = parseWhiteSequence( xObj );
  2554. // check if NL-character found
  2555. if( status == eScpiParserStatus_success )
  2556. {
  2557. if( eScpiEntityType_Service_MsgTerm == pObj->type )
  2558. {
  2559. goto L_parseArguments_TERM;
  2560. }
  2561. }
  2562. // restore tail pointer before searching for parameter
  2563. pObj->tail = _tail;
  2564. // move forward @head pointer to skip white sequence
  2565. pObj->head = pObj->str;
  2566. // check for end-of-string
  2567. if( eScpiParserStatus_success == checkNotEndOfString( xObj ) )
  2568. {
  2569. // call the parser
  2570. status = fParser( xObj );
  2571. // check result
  2572. if( status != eScpiParserStatus_success )
  2573. {
  2574. // parsing error
  2575. // check for specific error case: message separator (intercept)
  2576. if( SCPI_ERROR_INVALID_CHARACTER == pObj->error && scpi_ismsgsep( *pObj->head ) )
  2577. {
  2578. // replace "error condition" -> "message termination"
  2579. pObj->type = eScpiEntityType_Service_MsgTerm;
  2580. status = eScpiParserStatus_success;
  2581. goto L_parseArguments_TERM;
  2582. }
  2583. break;
  2584. }
  2585. else
  2586. {
  2587. trailingDataSep = false;
  2588. }
  2589. }
  2590. else
  2591. {
  2592. // end of string: error condition
  2593. pObj->type = eScpiEntityType_Service_MsgTerm;
  2594. goto L_parseArguments_TERM;
  2595. }
  2596. argTokens[nArgs].shead = pObj->head;
  2597. argTokens[nArgs].stail = pObj->tail;
  2598. if( NULL != entityTypes )
  2599. {
  2600. // write entity type to the output buffer
  2601. entityTypes[nArgs] = pObj->type;
  2602. }
  2603. nArgs++; // increment number of parsed parameters
  2604. // restore tail pointer before searching for separator
  2605. pObj->tail = _tail;
  2606. // move forward @head pointer to skip the separator
  2607. pObj->head = pObj->str;
  2608. // check for end-of-string
  2609. if( eScpiParserStatus_success == checkNotEndOfString( xObj ) )
  2610. {
  2611. status = parseProgramDataSeparator( xObj );
  2612. if( eScpiParserStatus_success != status )
  2613. {
  2614. // check for specific error case: message separator (intercept)
  2615. if( SCPI_ERROR_INVSEP_ERROR == pObj->error && scpi_ismsgsep( *pObj->head ) )
  2616. {
  2617. // replace "error condition" -> "message termination"
  2618. pObj->type = eScpiEntityType_Service_MsgTerm;
  2619. pObj->error = SCPI_ERROR_SUCCESS;
  2620. status = eScpiParserStatus_success;
  2621. goto L_parseArguments_TERM;
  2622. }
  2623. break;
  2624. }
  2625. }
  2626. else
  2627. {
  2628. pObj->type = eScpiEntityType_Service_MsgTerm;
  2629. goto L_parseArguments_TERM;
  2630. }
  2631. L_parseArguments_TERM:
  2632. switch( pObj->type )
  2633. {
  2634. case eScpiEntityType_Service_MsgTerm:
  2635. case eScpiEntityType_Service_MsgSep:
  2636. {
  2637. if( (nArgs) < argN - argOptionalN ) // not all mandatory parameters parsed
  2638. {
  2639. // error: missing parameter
  2640. status = eScpiParserStatus_failed;
  2641. pObj->head = pObj->tail = pObj->str = _tail; // reset all the token stats to not point to any token
  2642. pObj->error = SCPI_ERROR_MISSING_PARAMETER;
  2643. pObj->pErrorMsg = SCPI_ERROR_MISSING_PARAMETER_MSG;
  2644. }
  2645. else
  2646. {
  2647. // Check for trailing data separator
  2648. if( trailingDataSep )
  2649. {
  2650. // error: extra parameter
  2651. status = eScpiParserStatus_failed;
  2652. pObj->error = SCPI_ERROR_INVSEP_ERROR;
  2653. pObj->pErrorMsg = SCPI_ERROR_INVSEP_ERROR_MSG;
  2654. // reset all the token stats to not point to any token
  2655. pObj->head = pObj->tail = pObj->str = _tail;
  2656. }
  2657. }
  2658. (void)status;
  2659. parseBreak = true; // message terminator faced
  2660. }
  2661. break;
  2662. case eScpiEntityType_Service_DataSep:
  2663. {
  2664. if( nArgs >= argN ) // all parameters already parsed
  2665. {
  2666. // error: extra parameter
  2667. status = eScpiParserStatus_failed;
  2668. pObj->error = SCPI_ERROR_PARAMETER_NOTALLOWED;
  2669. pObj->pErrorMsg = SCPI_ERROR_PARAMETER_NOTALLOWED_MSG;
  2670. // reset all the token stats to not point to any token due to it is impossible
  2671. // to get know where is the end of extra parameter in general case (e.g. if the string is passed).
  2672. pObj->head = pObj->tail = pObj->str = _tail;
  2673. parseBreak = true;
  2674. }
  2675. trailingDataSep = true;
  2676. }
  2677. break;
  2678. default:
  2679. status = eScpiParserStatus_failed;
  2680. pObj->error = SCPI_ERROR_SYNTAX_ERROR;
  2681. pObj->pErrorMsg = SCPI_ERROR_SYNTAX_ERROR_PARAM_MSG;
  2682. }
  2683. // Restore iterators only if parsing is continuing.
  2684. // Otherwise it is required to leave @head/@tail pointing to the
  2685. // ... last found entity to search next command correctly.
  2686. if( ! parseBreak )
  2687. {
  2688. // restore iterators before searching next entity
  2689. pObj->head = pObj->str;
  2690. pObj->tail = _tail;
  2691. }
  2692. *pArgParsed = (nArgs); // indicate number of parameters parsed (success state)
  2693. }
  2694. return status;
  2695. }
  2696. eScpiParserStatus_t parseArrayArguments( xParseEntry_t * xObj,
  2697. uint8_t * argTokens,
  2698. size_t * argNumber,
  2699. const uint8_t * argTypes,
  2700. uint8_t * entityTypes,
  2701. size_t argN,
  2702. size_t argOptionalN,
  2703. uint8_t * pArgParsed )
  2704. {
  2705. my_assert( xObj );
  2706. my_assert( argTokens );
  2707. my_assert( pArgParsed );
  2708. my_assert( argN >= argOptionalN );
  2709. my_assert( argTypes || argN == 0 );
  2710. my_assert( argNumber );
  2711. sParseEntry_t * pObj = (sParseEntry_t*)xObj;
  2712. eScpiParserStatus_t status = eScpiParserStatus_invalid;
  2713. pObj->error = SCPI_ERROR_SYNTAX_ERROR;
  2714. pObj->pErrorMsg = SCPI_ERROR_SYNTAX_ERROR_PARAM_MSG;
  2715. // check parameters
  2716. if( NULL == xObj && NULL == argTokens && (NULL == argTypes && 0 != argN) && NULL == pArgParsed && (argN < argOptionalN) )
  2717. {
  2718. return eScpiParserStatus_invalid;
  2719. }
  2720. // special condition: no parameters are allowed
  2721. if( 0 == argN )
  2722. {
  2723. status = parseEndOfCommand( xObj );
  2724. if( status != eScpiParserStatus_success )
  2725. {
  2726. pObj->error = SCPI_ERROR_PARAMETER_NOTALLOWED;
  2727. pObj->pErrorMsg = SCPI_ERROR_PARAMETER_NOTALLOWED_MSG;
  2728. }
  2729. // no break, no return, 'for' will not be executed due to argN==0
  2730. }
  2731. bool parseBreak = false;
  2732. bool trailingDataSep = false;
  2733. size_t nArgs = 0;
  2734. for( size_t a = 0; (!parseBreak); ++a )
  2735. {
  2736. *pArgParsed = a; // indicate current parameter index (failed case)
  2737. eScpiParserStatus_t (*fParser)( xParseEntry_t * xObj ) = NULL;
  2738. // search for parser routine by the specified argument type
  2739. for( size_t f = 0; f < sizeof(parseArgumentsMap)/sizeof(*parseArgumentsMap); ++f )
  2740. {
  2741. if( parseArgumentsMap[f].type == argTypes[0] )
  2742. {
  2743. // parser found
  2744. fParser = parseArgumentsMap[f].f;
  2745. break;
  2746. }
  2747. }
  2748. if( NULL == fParser )
  2749. {
  2750. // parser not found
  2751. status = eScpiParserStatus_failed;
  2752. break;
  2753. }
  2754. // keep tail pointer before searching for white characters
  2755. const char * _tail = pObj->tail;
  2756. // skip white characters
  2757. status = parseWhiteSequence( xObj );
  2758. // check if NL-character found
  2759. if( status == eScpiParserStatus_success )
  2760. {
  2761. if( eScpiEntityType_Service_MsgTerm == pObj->type )
  2762. {
  2763. goto L_parseArguments_TERM;
  2764. }
  2765. }
  2766. // restore tail pointer before searching for parameter
  2767. pObj->tail = _tail;
  2768. // move forward @head pointer to skip white sequence
  2769. pObj->head = pObj->str;
  2770. // check for end-of-string
  2771. if( eScpiParserStatus_success == checkNotEndOfString( xObj ) )
  2772. {
  2773. // call the parser
  2774. status = fParser( xObj );
  2775. // check result
  2776. if( status != eScpiParserStatus_success )
  2777. {
  2778. // parsing error
  2779. // check for specific error case: message separator (intercept)
  2780. if( SCPI_ERROR_INVALID_CHARACTER == pObj->error && scpi_ismsgsep( *pObj->head ) )
  2781. {
  2782. // replace "error condition" -> "message termination"
  2783. pObj->type = eScpiEntityType_Service_MsgTerm;
  2784. status = eScpiParserStatus_success;
  2785. goto L_parseArguments_TERM;
  2786. }
  2787. break;
  2788. }
  2789. else
  2790. {
  2791. trailingDataSep = false;
  2792. }
  2793. }
  2794. else
  2795. {
  2796. // end of string: error condition
  2797. pObj->type = eScpiEntityType_Service_MsgTerm;
  2798. goto L_parseArguments_TERM;
  2799. }
  2800. {
  2801. sStrToken_t currentToken = {
  2802. .shead = pObj->head,
  2803. .stail = pObj->tail
  2804. };
  2805. sProcessNumericEntry_t Entry;
  2806. sNumericRange_t range =
  2807. {
  2808. .max.demicalInteger = 99,
  2809. .min.demicalInteger = 0,
  2810. .type = SCPI_NUMERIC_LIMIT_TYPE_I32
  2811. };
  2812. if(processNumericArgument( &Entry, &currentToken, pObj->type, &range )
  2813. && *argNumber > nArgs)
  2814. {
  2815. argTokens[nArgs] = Entry.valueEntry.Value.demicalInteger;
  2816. }
  2817. else
  2818. {
  2819. parseBreak = true;
  2820. goto L_parseArguments_TERM;
  2821. }
  2822. }
  2823. if( NULL != entityTypes )
  2824. {
  2825. // write entity type to the output buffer
  2826. entityTypes[nArgs] = pObj->type;
  2827. }
  2828. nArgs++; // increment number of parsed parameters
  2829. // restore tail pointer before searching for separator
  2830. pObj->tail = _tail;
  2831. // move forward @head pointer to skip the separator
  2832. pObj->head = pObj->str;
  2833. // check for end-of-string
  2834. if( eScpiParserStatus_success == checkNotEndOfString( xObj ) )
  2835. {
  2836. status = parseProgramDataSeparator( xObj );
  2837. if( eScpiParserStatus_success != status )
  2838. {
  2839. // check for specific error case: message separator (intercept)
  2840. if( SCPI_ERROR_INVSEP_ERROR == pObj->error && scpi_ismsgsep( *pObj->head ) )
  2841. {
  2842. // replace "error condition" -> "message termination"
  2843. pObj->type = eScpiEntityType_Service_MsgTerm;
  2844. pObj->error = SCPI_ERROR_SUCCESS;
  2845. status = eScpiParserStatus_success;
  2846. goto L_parseArguments_TERM;
  2847. }
  2848. break;
  2849. }
  2850. }
  2851. else
  2852. {
  2853. pObj->type = eScpiEntityType_Service_MsgTerm;
  2854. goto L_parseArguments_TERM;
  2855. }
  2856. L_parseArguments_TERM:
  2857. switch( pObj->type )
  2858. {
  2859. case eScpiEntityType_Service_MsgTerm:
  2860. case eScpiEntityType_Service_MsgSep:
  2861. {
  2862. if( (nArgs) < argN - argOptionalN ) // not all mandatory parameters parsed
  2863. {
  2864. // error: missing parameter
  2865. status = eScpiParserStatus_failed;
  2866. pObj->head = pObj->tail = pObj->str = _tail; // reset all the token stats to not point to any token
  2867. pObj->error = SCPI_ERROR_MISSING_PARAMETER;
  2868. pObj->pErrorMsg = SCPI_ERROR_MISSING_PARAMETER_MSG;
  2869. }
  2870. else
  2871. {
  2872. // Check for trailing data separator
  2873. if( trailingDataSep )
  2874. {
  2875. // error: extra parameter
  2876. status = eScpiParserStatus_failed;
  2877. pObj->error = SCPI_ERROR_INVSEP_ERROR;
  2878. pObj->pErrorMsg = SCPI_ERROR_INVSEP_ERROR_MSG;
  2879. // reset all the token stats to not point to any token
  2880. pObj->head = pObj->tail = pObj->str = _tail;
  2881. }
  2882. }
  2883. (void)status;
  2884. parseBreak = true; // message terminator faced
  2885. }
  2886. break;
  2887. case eScpiEntityType_Service_DataSep:
  2888. {
  2889. //if( nArgs >= argN ) // all parameters already parsed
  2890. //{
  2891. // error: extra parameter
  2892. //status = eScpiParserStatus_failed;
  2893. //pObj->error = SCPI_ERROR_PARAMETER_NOTALLOWED;
  2894. //pObj->pErrorMsg = SCPI_ERROR_PARAMETER_NOTALLOWED_MSG;
  2895. // reset all the token stats to not point to any token due to it is impossible
  2896. // to get know where is the end of extra parameter in general case (e.g. if the string is passed).
  2897. //pObj->head = pObj->tail = pObj->str = _tail;
  2898. //parseBreak = true;
  2899. //}
  2900. trailingDataSep = true;
  2901. }
  2902. break;
  2903. default:
  2904. status = eScpiParserStatus_failed;
  2905. pObj->error = SCPI_ERROR_SYNTAX_ERROR;
  2906. pObj->pErrorMsg = SCPI_ERROR_SYNTAX_ERROR_PARAM_MSG;
  2907. }
  2908. // Restore iterators only if parsing is continuing.
  2909. // Otherwise it is required to leave @head/@tail pointing to the
  2910. // ... last found entity to search next command correctly.
  2911. if( ! parseBreak )
  2912. {
  2913. // restore iterators before searching next entity
  2914. pObj->head = pObj->str;
  2915. pObj->tail = _tail;
  2916. }
  2917. *pArgParsed = (nArgs); // indicate number of parameters parsed (success state)
  2918. }
  2919. return status;
  2920. }
  2921. // =================================================================================================================
  2922. static xParseEntry_t ctx;
  2923. static char scpi_test_buffer[128] = {0};
  2924. static volatile bool scpi_test_exit = false;
  2925. void scpi_test_buf_example( size_t scpi_test_id,
  2926. size_t scpi_test_subid,
  2927. char ** _scpi_buffer_ptr,
  2928. size_t * _scpi_buffer_len,
  2929. size_t * _scpi_message_len,
  2930. bool * _scpi_end_of_message,
  2931. bool * _scpi_keep_ctx,
  2932. const char * * _entity_start,
  2933. size_t * _entity_length )
  2934. {
  2935. *_scpi_buffer_ptr = scpi_test_buffer;
  2936. *_scpi_buffer_len = 1;
  2937. *_scpi_end_of_message = false;
  2938. *_scpi_keep_ctx = false;
  2939. *_entity_start = NULL;
  2940. *_entity_length = 0;
  2941. *_scpi_message_len = 0;
  2942. switch( scpi_test_id )
  2943. {
  2944. case 3: // Numbers:
  2945. {
  2946. switch( scpi_test_subid )
  2947. {
  2948. // Correct examples:
  2949. case 0: strcpy( scpi_test_buffer, "3" ); break;
  2950. case 1: strcpy( scpi_test_buffer, "-3" ); break;
  2951. case 2: strcpy( scpi_test_buffer, ".3" ); break;
  2952. case 3: strcpy( scpi_test_buffer, "3.e+1" ); break;
  2953. case 4: strcpy( scpi_test_buffer, "3e14" ); break;
  2954. case 5: strcpy( scpi_test_buffer, "+3 e-1" ); break;
  2955. case 6: strcpy( scpi_test_buffer, "3 E 14" ); break;
  2956. case 7: strcpy( scpi_test_buffer, "-3.14e-159" ); break;
  2957. case 8: strcpy( scpi_test_buffer, "+3. E -14159" ); break;
  2958. case 9: strcpy( scpi_test_buffer, "3 E15 " ); break;
  2959. case 10: strcpy( scpi_test_buffer, "-.3 E -15 " ); break;
  2960. // Incorrect examples:
  2961. case 11: strcpy( scpi_test_buffer, "-3.1.e-1" ); break; // pass: 4 chars returned => actually succeeded
  2962. case 12: strcpy( scpi_test_buffer, "3e1.4" ); break; // pass: 3 chars returned => actually succeeded
  2963. case 13: strcpy( scpi_test_buffer, "+3. E -14159." ); break; // pass: 12 chars returned => actually succeeded
  2964. case 14: strcpy( scpi_test_buffer, "3 E15 E15" ); break; // pass: 5 chars returned => actually succeeded
  2965. case 15: strcpy( scpi_test_buffer, "-3.1-E -15 " ); break; // pass: 4 chars returned => actually succeeded
  2966. case 16: strcpy( scpi_test_buffer, " 3" ); break; // pass
  2967. case 17: strcpy( scpi_test_buffer, "+-3" ); break; // pass
  2968. case 18: strcpy( scpi_test_buffer, ". 3" ); break; // pass
  2969. case 19: strcpy( scpi_test_buffer, "3.e++1" ); break; // pass
  2970. case 20: strcpy( scpi_test_buffer, "3 E E 14" ); break; // pass
  2971. case 21: strcpy( scpi_test_buffer, "-3e.14e-159" ); break; // pass
  2972. case 22: strcpy( scpi_test_buffer, "3.e+e+1" ); break; // pass
  2973. case 23: strcpy( scpi_test_buffer, "3. E-E 14" ); break; // pass
  2974. case 24: strcpy( scpi_test_buffer, "-3e..1-159" ); break; // pass
  2975. case 25: strcpy( scpi_test_buffer, "3-.e+1" ); break; // pass: 1 chars returned => actually succeeded
  2976. case 26: strcpy( scpi_test_buffer, "3.E.+1E2" ); break; // pass
  2977. case 27: strcpy( scpi_test_buffer, "-3e.e1-159" ); break; // pass
  2978. }
  2979. *_scpi_message_len = strlen( scpi_test_buffer );
  2980. }
  2981. break;
  2982. case 4: // Non-demical numbers:
  2983. {
  2984. switch( scpi_test_subid )
  2985. {
  2986. // Correct examples:
  2987. case 0: strcpy( scpi_test_buffer, "#b00110001" ); break; // CORRECT : passed
  2988. case 1: strcpy( scpi_test_buffer, "#Haf020ffc" ); break; // CORRECT : passed
  2989. case 2: strcpy( scpi_test_buffer, "#Q7777111" ); break; // CORRECT : passed
  2990. case 3: strcpy( scpi_test_buffer, "b0011000" ); break; // INCORRECT: passed
  2991. case 4: strcpy( scpi_test_buffer, "Haf020ff" ); break; // INCORRECT: passed
  2992. case 5: strcpy( scpi_test_buffer, "q7777111" ); break; // INCORRECT: passed
  2993. case 6: strcpy( scpi_test_buffer, "0011000" ); break; // INCORRECT: passed
  2994. case 7: strcpy( scpi_test_buffer, "af020ff" ); break; // INCORRECT: passed
  2995. case 8: strcpy( scpi_test_buffer, "7777111" ); break; // INCORRECT: passed
  2996. case 9: strcpy( scpi_test_buffer, "##b0011000" ); break; // INCORRECT: passed
  2997. case 10:strcpy( scpi_test_buffer, "##haf020ff" ); break; // INCORRECT: passed
  2998. case 11:strcpy( scpi_test_buffer, "##q7777111" ); break; // INCORRECT: passed
  2999. case 12:strcpy( scpi_test_buffer, "#bb0011000" ); break; // INCORRECT: passed
  3000. case 13:strcpy( scpi_test_buffer, "#hhaf020ff" ); break; // INCORRECT: passed
  3001. case 14:strcpy( scpi_test_buffer, "#qq7777111" ); break; // INCORRECT: passed
  3002. }
  3003. *_scpi_message_len = strlen( scpi_test_buffer );
  3004. }
  3005. break;
  3006. }
  3007. }
  3008. void scpi_parser_test()
  3009. {
  3010. size_t scpi_test_id = 4;
  3011. // 0 = string
  3012. // 1 = arbitrary block
  3013. // 2 = command program header
  3014. // 3 = demical numbers
  3015. // 4 = non-demical numbers
  3016. eScpiParserStatus_t scpi_status = eScpiParserStatus_invalid;
  3017. size_t scpi_buffer_len = 1;// sizeof( scpi_test_buffer );
  3018. size_t scpi_message_len = sizeof(scpi_test_buffer);
  3019. char * scpi_buffer_ptr = scpi_test_buffer;
  3020. bool scpi_end_of_message = false;
  3021. bool scpi_keep_ctx = false;
  3022. const char * entity_start = NULL;
  3023. size_t entity_length = 0;
  3024. size_t scpi_test_subid = 0;
  3025. switch( scpi_test_id ) {
  3026. case 0: strcpy( scpi_test_buffer, "\"Test string with a \"\"quotted\"\" part.\"" ); break; //30
  3027. case 1: strcpy( scpi_test_buffer, "#2160123456789abcdef-----" ); break; //25
  3028. case 2: strcpy( scpi_test_buffer, ":MEM:TABLE:DATA PARAM" ); break; //26
  3029. case 3: scpi_test_buf_example ( scpi_test_id, scpi_test_subid,
  3030. &scpi_buffer_ptr,
  3031. &scpi_buffer_len,
  3032. &scpi_message_len,
  3033. &scpi_end_of_message,
  3034. &scpi_keep_ctx,
  3035. &entity_start,
  3036. &entity_length );
  3037. break;
  3038. case 4: scpi_test_buf_example ( scpi_test_id, scpi_test_subid,
  3039. &scpi_buffer_ptr,
  3040. &scpi_buffer_len,
  3041. &scpi_message_len,
  3042. &scpi_end_of_message,
  3043. &scpi_keep_ctx,
  3044. &entity_start,
  3045. &entity_length );
  3046. break;
  3047. }
  3048. asm( "bkpt #0" );
  3049. while( !scpi_test_exit )
  3050. {
  3051. scpi_end_of_message = ( scpi_buffer_ptr >= scpi_test_buffer + scpi_message_len - 1 );
  3052. if( ! prepareParserContext( &ctx,
  3053. scpi_buffer_ptr,
  3054. scpi_buffer_len,
  3055. scpi_end_of_message,
  3056. scpi_keep_ctx ) )
  3057. {
  3058. asm( "bkpt #0" );
  3059. break;
  3060. }
  3061. else
  3062. {
  3063. //asm( "bkpt #0" );
  3064. }
  3065. switch( scpi_test_id ) {
  3066. case 0: scpi_status = parseString( &ctx ); break;
  3067. case 1: scpi_status = parseArbitraryBlock( &ctx ); break;
  3068. case 2: scpi_status = parseCommandProgramHeader( &ctx ); break;
  3069. case 3: scpi_status = parseDemicalNumber( &ctx ); break;
  3070. case 4: scpi_status = parseNonDemicalNumber( &ctx ); break;
  3071. default: scpi_status = eScpiParserStatus_invalid;
  3072. }
  3073. switch( scpi_status )
  3074. {
  3075. case eScpiParserStatus_invalid:
  3076. {
  3077. scpi_keep_ctx = false;
  3078. asm( "bkpt #0" );
  3079. }
  3080. break;
  3081. case eScpiParserStatus_failed:
  3082. {
  3083. scpi_keep_ctx = false;
  3084. asm( "bkpt #0" );
  3085. scpi_test_buf_example ( scpi_test_id, ++scpi_test_subid,
  3086. &scpi_buffer_ptr,
  3087. &scpi_buffer_len,
  3088. &scpi_message_len,
  3089. &scpi_end_of_message,
  3090. &scpi_keep_ctx,
  3091. &entity_start,
  3092. &entity_length );
  3093. }
  3094. break;
  3095. case eScpiParserStatus_success:
  3096. {
  3097. const char * pEntityBegin = NULL;
  3098. const char * pEntityEnd = NULL;
  3099. uint8_t eEntityType;
  3100. scpi_keep_ctx = false;
  3101. getParsedEntityDetails( &ctx,
  3102. (void const**)&pEntityBegin,
  3103. (void const**)&pEntityEnd,
  3104. &eEntityType );
  3105. if( NULL == entity_start )
  3106. {
  3107. entity_start = pEntityBegin;
  3108. }
  3109. entity_length = (pEntityEnd - entity_start);
  3110. asm( "bkpt #0" );
  3111. scpi_test_buf_example ( scpi_test_id, ++scpi_test_subid,
  3112. &scpi_buffer_ptr,
  3113. &scpi_buffer_len,
  3114. &scpi_message_len,
  3115. &scpi_end_of_message,
  3116. &scpi_keep_ctx,
  3117. &entity_start,
  3118. &entity_length );
  3119. }
  3120. break;
  3121. case eScpiParserStatus_need_data:
  3122. {
  3123. const char * pEntityBegin = NULL;
  3124. const char * pEntityEnd = NULL;
  3125. scpi_keep_ctx = true;
  3126. getParsedEntityDetails( &ctx,
  3127. (void const**)&pEntityBegin,
  3128. (void const**)&pEntityEnd,
  3129. NULL );
  3130. scpi_buffer_ptr += (pEntityEnd - pEntityBegin);
  3131. //asm( "bkpt #0" );
  3132. if( NULL == entity_start )
  3133. {
  3134. entity_start = pEntityBegin;
  3135. }
  3136. }
  3137. break;
  3138. }
  3139. }
  3140. asm( "bkpt #0" );
  3141. }