You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

879 lines
22 KiB

15 years ago
4 months ago
4 months ago
4 months ago
4 months ago
15 years ago
15 years ago
15 years ago
15 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
15 years ago
15 years ago
13 years ago
13 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2007-2013 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  5. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #include <fast_float/fast_float.h>
  25. #include <cstdarg>
  26. #include <cstdio>
  27. #include <cstdlib> // bsearch()
  28. #include <cctype>
  29. #include <dsnlexer.h>
  30. #include <wx/translation.h>
  31. #define FMT_CLIPBOARD _( "clipboard" )
  32. //-----<DSNLEXER>-------------------------------------------------------------
  33. void DSNLEXER::init()
  34. {
  35. curTok = DSN_NONE;
  36. prevTok = DSN_NONE;
  37. stringDelimiter = '"';
  38. specctraMode = false;
  39. space_in_quoted_tokens = false;
  40. commentsAreTokens = false;
  41. SetKnowsBar( true ); // default since version 20240706
  42. curOffset = 0;
  43. }
  44. DSNLEXER::DSNLEXER( const KEYWORD* aKeywordTable, unsigned aKeywordCount, const KEYWORD_MAP* aKeywordMap,
  45. FILE* aFile, const wxString& aFilename ) :
  46. iOwnReaders( true ),
  47. start( nullptr ),
  48. next( nullptr ),
  49. limit( nullptr ),
  50. reader( nullptr ),
  51. specctraMode( false ),
  52. m_knowsBar( false ),
  53. space_in_quoted_tokens( false ),
  54. commentsAreTokens( false ),
  55. keywords( aKeywordTable ),
  56. keywordCount( aKeywordCount ),
  57. keywordsLookup( aKeywordMap )
  58. {
  59. PushReader( new FILE_LINE_READER( aFile, aFilename ) );
  60. init();
  61. }
  62. DSNLEXER::DSNLEXER( const KEYWORD* aKeywordTable, unsigned aKeywordCount, const KEYWORD_MAP* aKeywordMap,
  63. const std::string& aClipboardTxt, const wxString& aSource ) :
  64. iOwnReaders( true ),
  65. start( nullptr ),
  66. next( nullptr ),
  67. limit( nullptr ),
  68. reader( nullptr ),
  69. specctraMode( false ),
  70. m_knowsBar( false ),
  71. space_in_quoted_tokens( false ),
  72. commentsAreTokens( false ),
  73. keywords( aKeywordTable ),
  74. keywordCount( aKeywordCount ),
  75. keywordsLookup( aKeywordMap )
  76. {
  77. PushReader( new STRING_LINE_READER( aClipboardTxt, aSource.IsEmpty() ? wxString( FMT_CLIPBOARD )
  78. : aSource ) );
  79. init();
  80. }
  81. DSNLEXER::DSNLEXER( const KEYWORD* aKeywordTable, unsigned aKeywordCount, const KEYWORD_MAP* aKeywordMap,
  82. LINE_READER* aLineReader ) :
  83. iOwnReaders( false ),
  84. start( nullptr ),
  85. next( nullptr ),
  86. limit( nullptr ),
  87. reader( nullptr ),
  88. specctraMode( false ),
  89. m_knowsBar( false ),
  90. space_in_quoted_tokens( false ),
  91. commentsAreTokens( false ),
  92. keywords( aKeywordTable ),
  93. keywordCount( aKeywordCount ),
  94. keywordsLookup( aKeywordMap )
  95. {
  96. if( aLineReader )
  97. PushReader( aLineReader );
  98. init();
  99. }
  100. static const KEYWORD empty_keywords[1] = {};
  101. DSNLEXER::DSNLEXER( const std::string& aSExpression, const wxString& aSource ) :
  102. iOwnReaders( true ),
  103. start( nullptr ),
  104. next( nullptr ),
  105. limit( nullptr ),
  106. reader( nullptr ),
  107. specctraMode( false ),
  108. m_knowsBar( false ),
  109. space_in_quoted_tokens( false ),
  110. commentsAreTokens( false ),
  111. keywords( empty_keywords ),
  112. keywordCount( 0 ),
  113. keywordsLookup( nullptr )
  114. {
  115. PushReader( new STRING_LINE_READER( aSExpression, aSource.IsEmpty() ? wxString( FMT_CLIPBOARD )
  116. : aSource ) );
  117. init();
  118. }
  119. DSNLEXER::~DSNLEXER()
  120. {
  121. if( iOwnReaders )
  122. {
  123. // delete the LINE_READERs from the stack, since I own them.
  124. for( READER_STACK::iterator it = readerStack.begin(); it!=readerStack.end(); ++it )
  125. delete *it;
  126. }
  127. }
  128. void DSNLEXER::SetSpecctraMode( bool aMode )
  129. {
  130. specctraMode = aMode;
  131. if( aMode )
  132. {
  133. // specctra mode defaults, some of which can still be changed in this mode.
  134. space_in_quoted_tokens = true;
  135. }
  136. else
  137. {
  138. space_in_quoted_tokens = false;
  139. stringDelimiter = '"';
  140. }
  141. }
  142. void DSNLEXER::InitParserState()
  143. {
  144. curTok = DSN_NONE;
  145. prevTok = DSN_NONE;
  146. commentsAreTokens = false;
  147. curOffset = 0;
  148. }
  149. bool DSNLEXER::SyncLineReaderWith( DSNLEXER& aLexer )
  150. {
  151. // Synchronize the pointers handling the data read by the LINE_READER
  152. // only if aLexer shares the same LINE_READER, because only in this case
  153. // the char buffer is be common
  154. if( reader != aLexer.reader )
  155. return false;
  156. // We can synchronize the pointers which handle the data currently read
  157. start = aLexer.start;
  158. next = aLexer.next;
  159. limit = aLexer.limit;
  160. // Sync these parameters is not mandatory, but could help
  161. // for instance in debug
  162. curText = aLexer.curText;
  163. curOffset = aLexer.curOffset;
  164. return true;
  165. }
  166. void DSNLEXER::PushReader( LINE_READER* aLineReader )
  167. {
  168. readerStack.push_back( aLineReader );
  169. reader = aLineReader;
  170. start = (const char*) (*reader);
  171. // force a new readLine() as first thing.
  172. limit = start;
  173. next = start;
  174. }
  175. LINE_READER* DSNLEXER::PopReader()
  176. {
  177. LINE_READER* ret = nullptr;
  178. if( readerStack.size() )
  179. {
  180. ret = reader;
  181. readerStack.pop_back();
  182. if( readerStack.size() )
  183. {
  184. reader = readerStack.back();
  185. start = reader->Line();
  186. // force a new readLine() as first thing.
  187. limit = start;
  188. next = start;
  189. }
  190. else
  191. {
  192. reader = nullptr;
  193. start = dummy;
  194. limit = dummy;
  195. }
  196. }
  197. return ret;
  198. }
  199. int DSNLEXER::findToken( const std::string& tok ) const
  200. {
  201. if( keywordsLookup != nullptr )
  202. {
  203. KEYWORD_MAP::const_iterator it = keywordsLookup->find( tok.c_str() );
  204. if( it != keywordsLookup->end() )
  205. return it->second;
  206. }
  207. return DSN_SYMBOL; // not a keyword, some arbitrary symbol.
  208. }
  209. const char* DSNLEXER::Syntax( int aTok )
  210. {
  211. const char* ret;
  212. switch( aTok )
  213. {
  214. case DSN_NONE:
  215. ret = "NONE";
  216. break;
  217. case DSN_STRING_QUOTE:
  218. ret = "string_quote"; // a special DSN syntax token, see specctra spec.
  219. break;
  220. case DSN_QUOTE_DEF:
  221. ret = "quoted text delimiter";
  222. break;
  223. case DSN_DASH:
  224. ret = "-";
  225. break;
  226. case DSN_SYMBOL:
  227. ret = "symbol";
  228. break;
  229. case DSN_NUMBER:
  230. ret = "number";
  231. break;
  232. case DSN_RIGHT:
  233. ret = ")";
  234. break;
  235. case DSN_LEFT:
  236. ret = "(";
  237. break;
  238. case DSN_STRING:
  239. ret = "quoted string";
  240. break;
  241. case DSN_EOF:
  242. ret = "end of input";
  243. break;
  244. case DSN_BAR:
  245. ret = "|";
  246. break;
  247. default:
  248. ret = "???";
  249. }
  250. return ret;
  251. }
  252. const char* DSNLEXER::GetTokenText( int aTok ) const
  253. {
  254. const char* ret;
  255. if( aTok < 0 )
  256. return Syntax( aTok );
  257. else if( (unsigned) aTok < keywordCount )
  258. ret = keywords[aTok].name;
  259. else
  260. ret = "token too big";
  261. return ret;
  262. }
  263. wxString DSNLEXER::GetTokenString( int aTok ) const
  264. {
  265. wxString ret;
  266. ret << wxT("'") << wxString::FromUTF8( GetTokenText(aTok) ) << wxT("'");
  267. return ret;
  268. }
  269. bool DSNLEXER::IsSymbol( int aTok )
  270. {
  271. // This is static and not inline to reduce code space.
  272. // if aTok is >= 0, then it is a coincidental match to a keyword.
  273. return aTok == DSN_SYMBOL || aTok == DSN_STRING || aTok >= 0;
  274. }
  275. bool DSNLEXER::IsNumber( int aTok )
  276. {
  277. return aTok == DSN_NUMBER;
  278. }
  279. void DSNLEXER::Expecting( int aTok ) const
  280. {
  281. wxString errText = wxString::Format( _( "Expecting '%s'" ), GetTokenString( aTok ) );
  282. THROW_PARSE_ERROR( errText, CurSource(), CurLine(), CurLineNumber(), CurOffset() );
  283. }
  284. void DSNLEXER::Expecting( const char* text ) const
  285. {
  286. wxString errText =
  287. wxString::Format( _( "Expecting %s. Got '%s'" ), wxString::FromUTF8( text ), GetTokenString( CurTok() ) );
  288. THROW_PARSE_ERROR( errText, CurSource(), CurLine(), CurLineNumber(), CurOffset() );
  289. }
  290. void DSNLEXER::Unexpected( int aTok ) const
  291. {
  292. wxString errText = wxString::Format( _( "Unexpected '%s'" ), GetTokenString( aTok ) );
  293. THROW_PARSE_ERROR( errText, CurSource(), CurLine(), CurLineNumber(), CurOffset() );
  294. }
  295. void DSNLEXER::Duplicate( int aTok )
  296. {
  297. wxString errText = wxString::Format( _( "%s is a duplicate" ), GetTokenString( aTok ).GetData() );
  298. THROW_PARSE_ERROR( errText, CurSource(), CurLine(), CurLineNumber(), CurOffset() );
  299. }
  300. void DSNLEXER::Unexpected( const char* text ) const
  301. {
  302. wxString errText = wxString::Format( _( "Unexpected %s" ), wxString::FromUTF8( text ) );
  303. THROW_PARSE_ERROR( errText, CurSource(), CurLine(), CurLineNumber(), CurOffset() );
  304. }
  305. void DSNLEXER::NeedLEFT()
  306. {
  307. int tok = NextTok();
  308. if( tok != DSN_LEFT )
  309. Expecting( DSN_LEFT );
  310. }
  311. void DSNLEXER::NeedRIGHT()
  312. {
  313. int tok = NextTok();
  314. if( tok != DSN_RIGHT )
  315. Expecting( DSN_RIGHT );
  316. }
  317. void DSNLEXER::NeedBAR()
  318. {
  319. int tok = NextTok();
  320. if( tok != DSN_BAR )
  321. Expecting( DSN_BAR );
  322. }
  323. int DSNLEXER::NeedSYMBOL()
  324. {
  325. int tok = NextTok();
  326. if( !IsSymbol( tok ) )
  327. Expecting( DSN_SYMBOL );
  328. return tok;
  329. }
  330. int DSNLEXER::NeedSYMBOLorNUMBER()
  331. {
  332. int tok = NextTok();
  333. if( !IsSymbol( tok ) && !IsNumber( tok ) )
  334. Expecting( "a symbol or number" );
  335. return tok;
  336. }
  337. int DSNLEXER::NeedNUMBER( const char* aExpectation )
  338. {
  339. int tok = NextTok();
  340. if( !IsNumber( tok ) )
  341. {
  342. wxString errText = wxString::Format( _( "need a number for '%s'" ),
  343. wxString::FromUTF8( aExpectation ).GetData() );
  344. THROW_PARSE_ERROR( errText, CurSource(), CurLine(), CurLineNumber(), CurOffset() );
  345. }
  346. return tok;
  347. }
  348. /**
  349. * Test for whitespace.
  350. *
  351. * Our whitespace, by our definition, is a subset of ASCII, i.e. no bytes with MSB on can be
  352. * considered whitespace, since they are likely part of a multibyte UTF8 character.
  353. */
  354. static bool isSpace( char cc )
  355. {
  356. // cc is signed, so it is often negative.
  357. // Treat negative as large positive to exclude rapidly.
  358. if( (unsigned char) cc <= ' ' )
  359. {
  360. switch( (unsigned char) cc )
  361. {
  362. case ' ':
  363. case '\n':
  364. case '\r':
  365. case '\t':
  366. case '\0': // PCAD s-expression files have this.
  367. return true;
  368. }
  369. }
  370. return false;
  371. }
  372. inline bool isDigit( char cc )
  373. {
  374. return '0' <= cc && cc <= '9';
  375. }
  376. /// @return true if @a cc is an s-expression separator character.
  377. inline bool DSNLEXER::isSep( char cc )
  378. {
  379. return isSpace( cc ) || cc == '(' || cc == ')' || ( m_knowsBar && cc == '|' );
  380. }
  381. /**
  382. * Return true if the next sequence of text is a number:
  383. * either an integer, fixed point, or float with exponent. Stops scanning
  384. * at the first non-number character, even if it is not whitespace.
  385. *
  386. * @param cp is the start of the current token.
  387. * @param limit is the end of the current token.
  388. * @return true if input token is a number, else false.
  389. */
  390. static bool isNumber( const char* cp, const char* limit )
  391. {
  392. // regex for a float: "^[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?" i.e. any number,
  393. // code traversal manually here:
  394. bool sawNumber = false;
  395. if( cp < limit && ( *cp=='-' || *cp=='+' ) )
  396. ++cp;
  397. while( cp < limit && isDigit( *cp ) )
  398. {
  399. ++cp;
  400. sawNumber = true;
  401. }
  402. if( cp < limit && *cp == '.' )
  403. {
  404. ++cp;
  405. while( cp < limit && isDigit( *cp ) )
  406. {
  407. ++cp;
  408. sawNumber = true;
  409. }
  410. }
  411. if( sawNumber )
  412. {
  413. if( cp < limit && ( *cp=='E' || *cp=='e' ) )
  414. {
  415. ++cp;
  416. sawNumber = false; // exponent mandates at least one digit thereafter.
  417. if( cp < limit && ( *cp=='-' || *cp=='+' ) )
  418. ++cp;
  419. while( cp < limit && isDigit( *cp ) )
  420. {
  421. ++cp;
  422. sawNumber = true;
  423. }
  424. }
  425. }
  426. return sawNumber && cp==limit;
  427. }
  428. int DSNLEXER::NextTok()
  429. {
  430. const char* cur = next;
  431. const char* head = cur;
  432. prevTok = curTok;
  433. curSeparator.clear();
  434. if( curTok == DSN_EOF )
  435. goto exit;
  436. if( cur >= limit )
  437. {
  438. L_read:
  439. // blank lines are returned as "\n" and will have a len of 1.
  440. // EOF will have a len of 0 and so is detectable.
  441. int len = readLine();
  442. if( len == 0 )
  443. {
  444. cur = start; // after readLine(), since start can change, set cur offset to start
  445. curTok = DSN_EOF;
  446. goto exit;
  447. }
  448. cur = start; // after readLine() since start can change.
  449. // skip leading whitespace
  450. while( cur < limit && isSpace( *cur ) )
  451. {
  452. curSeparator += *cur;
  453. ++cur;
  454. }
  455. // If the first non-blank character is #, this line is a comment.
  456. // Comments cannot follow any other token on the same line.
  457. if( cur<limit && *cur=='#' )
  458. {
  459. if( commentsAreTokens )
  460. {
  461. // Grab the entire current line [excluding end of line char(s)] as the
  462. // current token. The '#' character may not be at offset zero.
  463. while( limit[-1] == '\n' || limit[-1] == '\r' )
  464. --limit;
  465. curText.clear();
  466. curText.append( start, limit );
  467. cur = start; // ensure a good curOffset below
  468. curTok = DSN_COMMENT;
  469. head = limit; // do a readLine() on next call in here.
  470. goto exit;
  471. }
  472. else
  473. {
  474. goto L_read;
  475. }
  476. }
  477. }
  478. else
  479. {
  480. // skip leading whitespace
  481. while( cur < limit && isSpace( *cur ) )
  482. {
  483. curSeparator += *cur;
  484. ++cur;
  485. }
  486. }
  487. if( cur >= limit )
  488. goto L_read;
  489. if( *cur == '(' )
  490. {
  491. curText = *cur;
  492. curTok = DSN_LEFT;
  493. head = cur+1;
  494. goto exit;
  495. }
  496. if( *cur == ')' )
  497. {
  498. curText = *cur;
  499. curTok = DSN_RIGHT;
  500. head = cur+1;
  501. goto exit;
  502. }
  503. if( m_knowsBar && *cur == '|' )
  504. {
  505. curText = *cur;
  506. curTok = DSN_BAR;
  507. head = cur+1;
  508. goto exit;
  509. }
  510. // Non-specctraMode, understands and deciphers escaped \, \r, \n, and \".
  511. // Strips off leading and trailing double quotes
  512. if( !specctraMode )
  513. {
  514. // a quoted string, will return DSN_STRING
  515. if( *cur == stringDelimiter )
  516. {
  517. // copy the token, character by character so we can remove doubled up quotes.
  518. curText.clear();
  519. ++cur; // skip over the leading delimiter, which is always " in non-specctraMode
  520. head = cur;
  521. while( head<limit )
  522. {
  523. // ESCAPE SEQUENCES:
  524. if( *head =='\\' )
  525. {
  526. char tbuf[8];
  527. char c;
  528. int i;
  529. if( ++head >= limit )
  530. break; // throw exception at L_unterminated
  531. switch( *head++ )
  532. {
  533. case '"':
  534. case '\\': c = head[-1]; break;
  535. case 'a': c = '\x07'; break;
  536. case 'b': c = '\x08'; break;
  537. case 'f': c = '\x0c'; break;
  538. case 'n': c = '\n'; break;
  539. case 'r': c = '\r'; break;
  540. case 't': c = '\x09'; break;
  541. case 'v': c = '\x0b'; break;
  542. case 'x': // 1 or 2 byte hex escape sequence
  543. for( i = 0; i < 2; ++i )
  544. {
  545. if( !isxdigit( head[i] ) )
  546. break;
  547. tbuf[i] = head[i];
  548. }
  549. tbuf[i] = '\0';
  550. if( i > 0 )
  551. c = (char) strtoul( tbuf, nullptr, 16 );
  552. else
  553. c = 'x'; // a goofed hex escape sequence, interpret as 'x'
  554. head += i;
  555. break;
  556. default: // 1-3 byte octal escape sequence
  557. --head;
  558. for( i=0; i<3; ++i )
  559. {
  560. if( head[i] < '0' || head[i] > '7' )
  561. break;
  562. tbuf[i] = head[i];
  563. }
  564. tbuf[i] = '\0';
  565. if( i > 0 )
  566. c = (char) strtoul( tbuf, nullptr, 8 );
  567. else
  568. c = '\\'; // a goofed octal escape sequence, interpret as '\'
  569. head += i;
  570. break;
  571. }
  572. curText += c;
  573. }
  574. else if( *head == '"' ) // end of the non-specctraMode DSN_STRING
  575. {
  576. curTok = DSN_STRING;
  577. ++head; // omit this trailing double quote
  578. goto exit;
  579. }
  580. else
  581. curText += *head++;
  582. } // while
  583. // L_unterminated:
  584. wxString errtxt( _( "Unterminated delimited string" ) );
  585. THROW_PARSE_ERROR( errtxt, CurSource(), CurLine(), CurLineNumber(),
  586. cur - start + curText.length() );
  587. }
  588. }
  589. else // is specctraMode, tests in this block should not occur in KiCad mode.
  590. {
  591. /* get the dash out of a <pin_reference> which is embedded for example
  592. like: U2-14 or "U2"-"14"
  593. This is detectable by a non-space immediately preceding the dash.
  594. */
  595. if( *cur == '-' && cur>start && !isSpace( cur[-1] ) )
  596. {
  597. curText = '-';
  598. curTok = DSN_DASH;
  599. head = cur+1;
  600. goto exit;
  601. }
  602. // switching the string_quote character
  603. if( prevTok == DSN_STRING_QUOTE )
  604. {
  605. static const wxString errtxt( _("String delimiter must be a single character of "
  606. "', \", or $") );
  607. char cc = *cur;
  608. switch( cc )
  609. {
  610. case '\'':
  611. case '$':
  612. case '"':
  613. break;
  614. default:
  615. THROW_PARSE_ERROR( errtxt, CurSource(), CurLine(), CurLineNumber(), CurOffset() );
  616. }
  617. curText = cc;
  618. head = cur+1;
  619. if( head<limit && !isSep( *head ) )
  620. {
  621. THROW_PARSE_ERROR( errtxt, CurSource(), CurLine(), CurLineNumber(), CurOffset() );
  622. }
  623. curTok = DSN_QUOTE_DEF;
  624. goto exit;
  625. }
  626. // specctraMode DSN_STRING
  627. if( *cur == stringDelimiter )
  628. {
  629. ++cur; // skip over the leading delimiter: ",', or $
  630. head = cur;
  631. while( head<limit && !isStringTerminator( *head ) )
  632. ++head;
  633. if( head >= limit )
  634. {
  635. wxString errtxt( _( "Un-terminated delimited string" ) );
  636. THROW_PARSE_ERROR( errtxt, CurSource(), CurLine(), CurLineNumber(), CurOffset() );
  637. }
  638. curText.clear();
  639. curText.append( cur, head );
  640. ++head; // skip over the trailing delimiter
  641. curTok = DSN_STRING;
  642. goto exit;
  643. }
  644. } // specctraMode
  645. // non-quoted token, read it into curText.
  646. curText.clear();
  647. head = cur;
  648. while( head<limit && !isSep( *head ) )
  649. curText += *head++;
  650. if( isNumber( curText.c_str(), curText.c_str() + curText.size() ) )
  651. {
  652. curTok = DSN_NUMBER;
  653. goto exit;
  654. }
  655. if( specctraMode && curText == "string_quote" )
  656. {
  657. curTok = DSN_STRING_QUOTE;
  658. goto exit;
  659. }
  660. curTok = findToken( curText );
  661. exit: // single point of exit, no returns elsewhere please.
  662. curOffset = cur - start;
  663. next = head;
  664. return curTok;
  665. }
  666. wxArrayString* DSNLEXER::ReadCommentLines()
  667. {
  668. wxArrayString* ret = nullptr;
  669. bool cmt_setting = SetCommentsAreTokens( true );
  670. int tok = NextTok();
  671. if( tok == DSN_COMMENT )
  672. {
  673. ret = new wxArrayString();
  674. do
  675. {
  676. ret->Add( FromUTF8() );
  677. }
  678. while( ( tok = NextTok() ) == DSN_COMMENT );
  679. }
  680. SetCommentsAreTokens( cmt_setting );
  681. return ret;
  682. }
  683. double DSNLEXER::parseDouble()
  684. {
  685. // Use fast_float::from_chars which is designed to be locale independent and significantly
  686. // faster than strtod and std::from_chars
  687. const std::string& str = CurStr();
  688. double dval{};
  689. fast_float::from_chars_result res = fast_float::from_chars( str.data(), str.data() + str.size(), dval,
  690. fast_float::chars_format::skip_white_space );
  691. if( res.ec != std::errc() )
  692. {
  693. THROW_PARSE_ERROR( _( "Invalid floating point number" ), CurSource(), CurLine(),
  694. CurLineNumber(), CurOffset() );
  695. }
  696. return dval;
  697. }