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.

829 lines
20 KiB

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