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.

1504 lines
33 KiB

  1. /*
  2. * This program source code file is part of KICAD, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2007-2008 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  5. * Copyright (C) 2007 Kicad Developers, see change_log.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 "fctsys.h"
  30. #include "pcbnew.h"
  31. //#define STANDALONE 1 // enable this for stand alone testing.
  32. static int compare( const void* a1, const void* a2 )
  33. {
  34. const KEYWORD* k1 = (const KEYWORD*) a1;
  35. const KEYWORD* k2 = (const KEYWORD*) a2;
  36. int ret = strcmp( k1->name, k2->name );
  37. return ret;
  38. }
  39. //-----<DSNLEXER>-------------------------------------------------------------
  40. void DSNLEXER::init()
  41. {
  42. curTok = DSN_NONE;
  43. stringDelimiter = '"';
  44. space_in_quoted_tokens = true;
  45. commentsAreTokens = false;
  46. // "start" should never change until we change the reader. The DSN
  47. // format spec supports an include file mechanism but we can add that later
  48. // using a std::stack to hold a stack of LINE_READERs to track nesting.
  49. start = (char*) (*reader);
  50. limit = start;
  51. next = start;
  52. }
  53. DSNLEXER::DSNLEXER( FILE* aFile, const wxString& aFilename,
  54. const KEYWORD* aKeywordTable, unsigned aKeywordCount ) :
  55. keywords( aKeywordTable ),
  56. keywordCount( aKeywordCount )
  57. {
  58. filename = aFilename;
  59. reader = new FILE_LINE_READER( aFile, 4096 );
  60. init();
  61. }
  62. DSNLEXER::DSNLEXER( const std::string& aClipboardTxt,
  63. const KEYWORD* aKeywordTable, unsigned aKeywordCount ) :
  64. keywords( aKeywordTable ),
  65. keywordCount( aKeywordCount )
  66. {
  67. filename = _( "clipboard" );
  68. reader = new STRING_LINE_READER( aClipboardTxt );
  69. init();
  70. }
  71. int DSNLEXER::findToken( const std::string& tok )
  72. {
  73. // convert to lower case once, this should be faster than using strcasecmp()
  74. // for each test in compare().
  75. lowercase.clear();
  76. for( std::string::const_iterator iter = tok.begin(); iter!=tok.end(); ++iter )
  77. lowercase += (char) tolower( *iter );
  78. KEYWORD search;
  79. search.name = lowercase.c_str();
  80. // a boost hashtable might be a few percent faster, depending on
  81. // hashtable size and quality of the hash function.
  82. const KEYWORD* findings = (const KEYWORD*) bsearch( &search,
  83. keywords, keywordCount,
  84. sizeof(KEYWORD), compare );
  85. if( findings )
  86. return findings->token;
  87. else
  88. return -1;
  89. }
  90. const char* DSNLEXER::Syntax( int aTok )
  91. {
  92. const char* ret;
  93. switch( aTok )
  94. {
  95. case DSN_NONE:
  96. ret = "NONE";
  97. break;
  98. case DSN_STRING_QUOTE:
  99. ret = "string_quote"; // a special DSN syntax token, see specctra spec.
  100. break;
  101. case DSN_QUOTE_DEF:
  102. ret = "quoted text delimiter";
  103. break;
  104. case DSN_DASH:
  105. ret = "-";
  106. break;
  107. case DSN_SYMBOL:
  108. ret = "symbol";
  109. break;
  110. case DSN_NUMBER:
  111. ret = "number";
  112. break;
  113. case DSN_RIGHT:
  114. ret = ")";
  115. break;
  116. case DSN_LEFT:
  117. ret = "(";
  118. break;
  119. case DSN_STRING:
  120. ret = "quoted string";
  121. break;
  122. case DSN_EOF:
  123. ret = "end of file";
  124. break;
  125. default:
  126. ret = "???";
  127. }
  128. return ret;
  129. }
  130. const char* DSNLEXER::GetTokenText( int aTok )
  131. {
  132. const char* ret;
  133. if( aTok < 0 )
  134. {
  135. return Syntax( aTok );
  136. }
  137. else if( (unsigned) aTok < keywordCount )
  138. {
  139. ret = keywords[aTok].name;
  140. }
  141. else
  142. ret = "token too big";
  143. return ret;
  144. }
  145. wxString DSNLEXER::GetTokenString( int aTok )
  146. {
  147. wxString ret;
  148. ret << wxT("'") << CONV_FROM_UTF8( GetTokenText(aTok) ) << wxT("'");
  149. return ret;
  150. }
  151. bool DSNLEXER::IsSymbol( int aTok )
  152. {
  153. // This is static and not inline to reduce code space.
  154. // if aTok is >= 0, then it is a coincidental match to a keyword.
  155. return aTok==DSN_SYMBOL
  156. || aTok==DSN_STRING
  157. || aTok>=0
  158. ;
  159. }
  160. void DSNLEXER::ThrowIOError( wxString aText, int charOffset ) throw (IOError)
  161. {
  162. // append to aText, do not overwrite
  163. aText << wxT(" ") << _("in") << wxT(" \"") << filename
  164. << wxT("\" ") << _("on line") << wxT(" ") << reader->LineNumber()
  165. << wxT(" ") << _("at offset") << wxT(" ") << charOffset;
  166. throw IOError( aText );
  167. }
  168. void DSNLEXER::Expecting( int aTok ) throw( IOError )
  169. {
  170. wxString errText( _("Expecting") );
  171. errText << wxT(" ") << GetTokenString( aTok );
  172. ThrowIOError( errText, CurOffset() );
  173. }
  174. void DSNLEXER::Expecting( const wxString& text ) throw( IOError )
  175. {
  176. wxString errText( _("Expecting") );
  177. errText << wxT(" '") << text << wxT("'");
  178. ThrowIOError( errText, CurOffset() );
  179. }
  180. void DSNLEXER::Unexpected( int aTok ) throw( IOError )
  181. {
  182. wxString errText( _("Unexpected") );
  183. errText << wxT(" ") << GetTokenString( aTok );
  184. ThrowIOError( errText, CurOffset() );
  185. }
  186. void DSNLEXER::Unexpected( const wxString& text ) throw( IOError )
  187. {
  188. wxString errText( _("Unexpected") );
  189. errText << wxT(" '") << text << wxT("'");
  190. ThrowIOError( errText, CurOffset() );
  191. }
  192. void DSNLEXER::NeedLEFT() throw( IOError )
  193. {
  194. int tok = NextTok();
  195. if( tok != DSN_LEFT )
  196. Expecting( DSN_LEFT );
  197. }
  198. void DSNLEXER::NeedRIGHT() throw( IOError )
  199. {
  200. int tok = NextTok();
  201. if( tok != DSN_RIGHT )
  202. Expecting( DSN_RIGHT );
  203. }
  204. int DSNLEXER::NeedSYMBOL() throw( IOError )
  205. {
  206. int tok = NextTok();
  207. if( !IsSymbol( tok ) )
  208. Expecting( DSN_SYMBOL );
  209. return tok;
  210. }
  211. int DSNLEXER::NeedSYMBOLorNUMBER() throw( IOError )
  212. {
  213. int tok = NextTok();
  214. if( !IsSymbol( tok ) && tok!=DSN_NUMBER )
  215. Expecting( _("symbol|number") );
  216. return tok;
  217. }
  218. /**
  219. * Function isspace
  220. * strips the upper bits of the int to ensure the value passed to ::isspace() is
  221. * in the range of 0-255
  222. */
  223. static inline bool isSpace( int cc )
  224. {
  225. // make sure int passed to ::isspace() is 0-255
  226. return ::isspace( cc & 0xff );
  227. }
  228. int DSNLEXER::NextTok() throw (IOError)
  229. {
  230. char* cur = next;
  231. char* head = cur;
  232. prevTok = curTok;
  233. if( curTok != DSN_EOF )
  234. {
  235. if( cur >= limit )
  236. {
  237. L_read:
  238. // blank lines are returned as "\n" and will have a len of 1.
  239. // EOF will have a len of 0 and so is detectable.
  240. int len = readLine();
  241. if( len == 0 )
  242. {
  243. curTok = DSN_EOF;
  244. goto exit;
  245. }
  246. cur = start;
  247. // skip leading whitespace
  248. while( cur<limit && isSpace(*cur) )
  249. ++cur;
  250. // If the first non-blank character is #, this line is a comment.
  251. // Comments cannot follow any other token on the same line.
  252. if( cur<limit && *cur=='#' )
  253. {
  254. if( commentsAreTokens )
  255. {
  256. // save the entire line, including new line as the current token.
  257. // the '#' character may not be at offset zero.
  258. curText = start; // entire line is the token
  259. cur = start; // ensure a good curOffset below
  260. curTok = DSN_COMMENT;
  261. head = limit; // do a readLine() on next call in here.
  262. goto exit;
  263. }
  264. else
  265. goto L_read;
  266. }
  267. }
  268. else
  269. {
  270. // skip leading whitespace
  271. while( cur<limit && isSpace(*cur) )
  272. ++cur;
  273. }
  274. if( cur >= limit )
  275. goto L_read;
  276. // switching the string_quote character
  277. if( prevTok == DSN_STRING_QUOTE )
  278. {
  279. static const wxString errtxt( _("String delimiter must be a single character of ', \", or $"));
  280. char cc = *cur;
  281. switch( cc )
  282. {
  283. case '\'':
  284. case '$':
  285. case '"':
  286. break;
  287. default:
  288. ThrowIOError( errtxt, CurOffset() );
  289. }
  290. curText = cc;
  291. head = cur+1;
  292. if( head<limit && *head!=')' && *head!='(' && !isSpace(*head) )
  293. {
  294. ThrowIOError( errtxt, CurOffset() );
  295. }
  296. curTok = DSN_QUOTE_DEF;
  297. goto exit;
  298. }
  299. if( *cur == '(' )
  300. {
  301. curText = *cur;
  302. curTok = DSN_LEFT;
  303. head = cur+1;
  304. goto exit;
  305. }
  306. if( *cur == ')' )
  307. {
  308. curText = *cur;
  309. curTok = DSN_RIGHT;
  310. head = cur+1;
  311. goto exit;
  312. }
  313. /* get the dash out of a <pin_reference> which is embedded for example
  314. like: U2-14 or "U2"-"14"
  315. This is detectable by a non-space immediately preceeding the dash.
  316. */
  317. if( *cur == '-' && cur>start && !isSpace( cur[-1] ) )
  318. {
  319. curText = '-';
  320. curTok = DSN_DASH;
  321. head = cur+1;
  322. goto exit;
  323. }
  324. // handle DSN_NUMBER
  325. if( strchr( "+-.0123456789", *cur ) )
  326. {
  327. head = cur+1;
  328. while( head<limit && strchr( ".0123456789", *head ) )
  329. ++head;
  330. if( (head<limit && isSpace(*head)) || *head==')' || *head=='(' || head==limit )
  331. {
  332. curText.clear();
  333. curText.append( cur, head );
  334. curTok = DSN_NUMBER;
  335. goto exit;
  336. }
  337. // else it was something like +5V, fall through below
  338. }
  339. // a quoted string
  340. if( *cur == stringDelimiter )
  341. {
  342. // New code, understands nested quotes, and deliberately restricts
  343. // strings to a single line. Still strips off leading and trailing
  344. // quotes, and now removes internal doubled up quotes
  345. #if 1
  346. head = cur;
  347. // copy the token, character by character so we can remove doubled up quotes.
  348. curText.clear();
  349. while( head < limit )
  350. {
  351. if( *head==stringDelimiter )
  352. {
  353. if( head+1<limit && head[1]==stringDelimiter )
  354. {
  355. // include only one of the doubled-up stringDelimiters
  356. curText += *head;
  357. head += 2;
  358. continue;
  359. }
  360. else if( head == cur )
  361. {
  362. ++head; // skip the leading quote
  363. continue;
  364. }
  365. // fall thru
  366. }
  367. // check for a terminator
  368. if( isStringTerminator( *head ) )
  369. {
  370. curTok = DSN_STRING;
  371. ++head;
  372. goto exit;
  373. }
  374. curText += *head++;
  375. }
  376. wxString errtxt(_("Un-terminated delimited string") );
  377. ThrowIOError( errtxt, CurOffset() );
  378. #else // old code, did not understand nested quotes
  379. ++cur; // skip over the leading delimiter: ",', or $
  380. head = cur;
  381. while( head<limit && !isStringTerminator( *head ) )
  382. ++head;
  383. if( head >= limit )
  384. {
  385. wxString errtxt(_("Un-terminated delimited string") );
  386. ThrowIOError( errtxt, CurOffset() );
  387. }
  388. curText.clear();
  389. curText.append( cur, head );
  390. ++head; // skip over the trailing delimiter
  391. curTok = DSN_STRING;
  392. goto exit;
  393. #endif
  394. }
  395. // Maybe it is a token we will find in the token table.
  396. // If not, then call it a DSN_SYMBOL.
  397. {
  398. head = cur+1;
  399. while( head<limit && !isSpace( *head ) && *head!=')' && *head!='(' )
  400. ++head;
  401. curText.clear();
  402. curText.append( cur, head );
  403. int found = findToken( curText );
  404. if( found != -1 )
  405. curTok = found;
  406. else if( 0 == curText.compare( "string_quote" ) )
  407. curTok = DSN_STRING_QUOTE;
  408. else // unrecogized token, call it a symbol
  409. curTok = DSN_SYMBOL;
  410. }
  411. }
  412. exit: // single point of exit, no returns elsewhere please.
  413. curOffset = cur - start;
  414. next = head;
  415. // printf("tok:\"%s\"\n", curText.c_str() );
  416. return curTok;
  417. }
  418. // stand alone testing
  419. #if defined(STANDALONE)
  420. #include <wx/dataobj.h>
  421. #include <wx/clipbrd.h>
  422. enum TEST_T {
  423. // these first few are negative special ones for syntax, and are
  424. // inherited from DSNLEXER.
  425. T_NONE = DSN_NONE,
  426. T_STRING_QUOTE = DSN_STRING_QUOTE,
  427. T_QUOTE_DEF = DSN_QUOTE_DEF,
  428. T_DASH = DSN_DASH,
  429. T_SYMBOL = DSN_SYMBOL,
  430. T_NUMBER = DSN_NUMBER,
  431. T_RIGHT = DSN_RIGHT, // right bracket, ')'
  432. T_LEFT = DSN_LEFT, // left bracket, '('
  433. T_STRING = DSN_STRING, // a quoted string, stripped of the quotes
  434. T_EOF = DSN_EOF, // special case for end of file
  435. // This should be coordinated with the
  436. // const static KEYWORD tokens[] array, and both must be sorted
  437. // identically and alphabetically. Remember that '_' is less than any
  438. // alpha character according to ASCII.
  439. T_absolute = 0, // this one should be == zero
  440. T_added,
  441. T_add_group,
  442. T_add_pins,
  443. T_allow_antenna,
  444. T_allow_redundant_wiring,
  445. T_amp,
  446. T_ancestor,
  447. T_antipad,
  448. T_aperture_type,
  449. T_array,
  450. T_attach,
  451. T_attr,
  452. T_average_pair_length,
  453. T_back,
  454. T_base_design,
  455. T_bbv_ctr2ctr,
  456. T_bend_keepout,
  457. T_bond,
  458. T_both,
  459. T_bottom,
  460. T_bottom_layer_sel,
  461. T_boundary,
  462. T_brickpat,
  463. T_bundle,
  464. T_bus,
  465. T_bypass,
  466. T_capacitance_resolution,
  467. T_capacitor,
  468. T_case_sensitive,
  469. T_cct1,
  470. T_cct1a,
  471. T_center_center,
  472. T_checking_trim_by_pin,
  473. T_circ,
  474. T_circle,
  475. T_circuit,
  476. T_class,
  477. T_class_class,
  478. T_classes,
  479. T_clear,
  480. T_clearance,
  481. T_cluster,
  482. T_cm,
  483. T_color,
  484. T_colors,
  485. T_comment,
  486. T_comp,
  487. T_comp_edge_center,
  488. T_comp_order,
  489. T_component,
  490. T_composite,
  491. T_conductance_resolution,
  492. T_conductor,
  493. T_conflict,
  494. T_connect,
  495. T_constant,
  496. T_contact,
  497. T_control,
  498. T_corner,
  499. T_corners,
  500. T_cost,
  501. T_created_time,
  502. T_cross,
  503. T_crosstalk_model,
  504. T_current_resolution,
  505. T_delete_pins,
  506. T_deleted,
  507. T_deleted_keepout,
  508. T_delta,
  509. T_diagonal,
  510. T_direction,
  511. T_directory,
  512. T_discrete,
  513. T_effective_via_length,
  514. T_elongate_keepout,
  515. T_exclude,
  516. T_expose,
  517. T_extra_image_directory,
  518. T_family,
  519. T_family_family,
  520. T_family_family_spacing,
  521. T_fanout,
  522. T_farad,
  523. T_file,
  524. T_fit,
  525. T_fix,
  526. T_flip_style,
  527. T_floor_plan,
  528. T_footprint,
  529. T_forbidden,
  530. T_force_to_terminal_point,
  531. T_free,
  532. T_forgotten,
  533. T_fromto,
  534. T_front,
  535. T_front_only,
  536. T_gap,
  537. T_gate,
  538. T_gates,
  539. T_generated_by_freeroute,
  540. T_global,
  541. T_grid,
  542. T_group,
  543. T_group_set,
  544. T_guide,
  545. T_hard,
  546. T_height,
  547. T_high,
  548. T_history,
  549. T_horizontal,
  550. T_host_cad,
  551. T_host_version,
  552. T_image,
  553. T_image_conductor,
  554. T_image_image,
  555. T_image_image_spacing,
  556. T_image_outline_clearance,
  557. T_image_set,
  558. T_image_type,
  559. T_inch,
  560. T_include,
  561. T_include_pins_in_crosstalk,
  562. T_inductance_resolution,
  563. T_insert,
  564. T_instcnfg,
  565. T_inter_layer_clearance,
  566. T_jumper,
  567. T_junction_type,
  568. T_keepout,
  569. T_kg,
  570. T_kohm,
  571. T_large,
  572. T_large_large,
  573. T_layer,
  574. T_layer_depth,
  575. T_layer_noise_weight,
  576. T_layer_pair,
  577. T_layer_rule,
  578. T_length,
  579. T_length_amplitude,
  580. T_length_factor,
  581. T_length_gap,
  582. T_library,
  583. T_library_out,
  584. T_limit,
  585. T_limit_bends,
  586. T_limit_crossing,
  587. T_limit_vias,
  588. T_limit_way,
  589. T_linear,
  590. T_linear_interpolation,
  591. T_load,
  592. T_lock_type,
  593. T_logical_part,
  594. T_logical_part_mapping,
  595. T_low,
  596. T_match_fromto_delay,
  597. T_match_fromto_length,
  598. T_match_group_delay,
  599. T_match_group_length,
  600. T_match_net_delay,
  601. T_match_net_length,
  602. T_max_delay,
  603. T_max_len,
  604. T_max_length,
  605. T_max_noise,
  606. T_max_restricted_layer_length,
  607. T_max_stagger,
  608. T_max_stub,
  609. T_max_total_delay,
  610. T_max_total_length,
  611. T_max_total_vias,
  612. T_medium,
  613. T_mhenry,
  614. T_mho,
  615. T_microvia,
  616. T_mid_driven,
  617. T_mil,
  618. T_min_gap,
  619. T_mirror,
  620. T_mirror_first,
  621. T_mixed,
  622. T_mm,
  623. T_negative_diagonal,
  624. T_net,
  625. T_net_number,
  626. T_net_out,
  627. T_net_pin_changes,
  628. T_nets,
  629. T_network,
  630. T_network_out,
  631. T_no,
  632. T_noexpose,
  633. T_noise_accumulation,
  634. T_noise_calculation,
  635. T_normal,
  636. T_object_type,
  637. T_off,
  638. T_off_grid,
  639. T_offset,
  640. T_on,
  641. T_open,
  642. T_opposite_side,
  643. T_order,
  644. T_orthogonal,
  645. T_outline,
  646. T_overlap,
  647. T_pad,
  648. T_pad_pad,
  649. T_padstack,
  650. T_pair,
  651. T_parallel,
  652. T_parallel_noise,
  653. T_parallel_segment,
  654. T_parser,
  655. T_part_library,
  656. T_path,
  657. T_pcb,
  658. T_permit_orient,
  659. T_permit_side,
  660. T_physical,
  661. T_physical_part_mapping,
  662. T_piggyback,
  663. T_pin,
  664. T_pin_allow,
  665. T_pin_cap_via,
  666. T_pin_via_cap,
  667. T_pin_width_taper,
  668. T_pins,
  669. T_pintype,
  670. T_place,
  671. T_place_boundary,
  672. T_place_control,
  673. T_place_keepout,
  674. T_place_rule,
  675. T_placement,
  676. T_plan,
  677. T_plane,
  678. T_pn,
  679. T_point,
  680. T_polyline_path,
  681. T_polygon,
  682. T_position,
  683. T_positive_diagonal,
  684. T_power,
  685. T_power_dissipation,
  686. T_power_fanout,
  687. T_prefix,
  688. T_primary,
  689. T_priority,
  690. T_property,
  691. T_protect,
  692. T_qarc,
  693. T_quarter,
  694. T_radius,
  695. T_ratio,
  696. T_ratio_tolerance,
  697. T_rect,
  698. T_reduced,
  699. T_region,
  700. T_region_class,
  701. T_region_class_class,
  702. T_region_net,
  703. T_relative_delay,
  704. T_relative_group_delay,
  705. T_relative_group_length,
  706. T_relative_length,
  707. T_reorder,
  708. T_reroute_order_viols,
  709. T_resistance_resolution,
  710. T_resistor,
  711. T_resolution,
  712. T_restricted_layer_length_factor,
  713. T_room,
  714. T_rotate,
  715. T_rotate_first,
  716. T_round,
  717. T_roundoff_rotation,
  718. T_route,
  719. T_route_to_fanout_only,
  720. T_routes,
  721. T_routes_include,
  722. T_rule,
  723. T_same_net_checking,
  724. T_sample_window,
  725. T_saturation_length,
  726. T_sec,
  727. T_secondary,
  728. T_self,
  729. T_sequence_number,
  730. T_session,
  731. T_set_color,
  732. T_set_pattern,
  733. T_shape,
  734. T_shield,
  735. T_shield_gap,
  736. T_shield_loop,
  737. T_shield_tie_down_interval,
  738. T_shield_width,
  739. T_side,
  740. T_signal,
  741. T_site,
  742. T_small,
  743. T_smd,
  744. T_snap,
  745. T_snap_angle,
  746. T_soft,
  747. T_source,
  748. T_space_in_quoted_tokens,
  749. T_spacing,
  750. T_spare,
  751. T_spiral_via,
  752. T_square,
  753. T_stack_via,
  754. T_stack_via_depth,
  755. T_standard,
  756. T_starburst,
  757. T_status,
  758. T_structure,
  759. T_structure_out,
  760. T_subgate,
  761. T_subgates,
  762. T_substituted,
  763. T_such,
  764. T_suffix,
  765. T_super_placement,
  766. T_supply,
  767. T_supply_pin,
  768. T_swapping,
  769. T_switch_window,
  770. T_system,
  771. T_tandem_noise,
  772. T_tandem_segment,
  773. T_tandem_shield_overhang,
  774. T_terminal,
  775. T_terminator,
  776. T_term_only,
  777. T_test,
  778. T_test_points,
  779. T_testpoint,
  780. T_threshold,
  781. T_time_length_factor,
  782. T_time_resolution,
  783. T_tjunction,
  784. T_tolerance,
  785. T_top,
  786. T_topology,
  787. T_total,
  788. T_track_id,
  789. T_turret,
  790. T_type,
  791. T_um,
  792. T_unassigned,
  793. T_unconnects,
  794. T_unit,
  795. T_up,
  796. T_use_array,
  797. T_use_layer,
  798. T_use_net,
  799. T_use_via,
  800. T_value,
  801. T_vertical,
  802. T_via,
  803. T_via_array_template,
  804. T_via_at_smd,
  805. T_via_keepout,
  806. T_via_number,
  807. T_via_rotate_first,
  808. T_via_site,
  809. T_via_size,
  810. T_virtual_pin,
  811. T_volt,
  812. T_voltage_resolution,
  813. T_was_is,
  814. T_way,
  815. T_weight,
  816. T_width,
  817. T_window,
  818. T_wire,
  819. T_wire_keepout,
  820. T_wires,
  821. T_wires_include,
  822. T_wiring,
  823. T_write_resolution,
  824. T_x,
  825. T_xy,
  826. T_y,
  827. };
  828. #define TOKDEF(x) { #x, T_##x }
  829. // This MUST be sorted alphabetically, and the order of enum DSN_T {} be
  830. // identially alphabetized. These MUST all be lower case because of the
  831. // conversion to lowercase in findToken().
  832. static const KEYWORD keywords[] = {
  833. // Note that TOKDEF(string_quote) has been moved to the
  834. // DSNLEXER, and DSN_SYNTAX_T enum, and the string for it is "string_quote".
  835. TOKDEF(absolute),
  836. TOKDEF(added),
  837. TOKDEF(add_group),
  838. TOKDEF(add_pins),
  839. TOKDEF(allow_antenna),
  840. TOKDEF(allow_redundant_wiring),
  841. TOKDEF(amp),
  842. TOKDEF(ancestor),
  843. TOKDEF(antipad),
  844. TOKDEF(aperture_type),
  845. TOKDEF(array),
  846. TOKDEF(attach),
  847. TOKDEF(attr),
  848. TOKDEF(average_pair_length),
  849. TOKDEF(back),
  850. TOKDEF(base_design),
  851. TOKDEF(bbv_ctr2ctr),
  852. TOKDEF(bend_keepout),
  853. TOKDEF(bond),
  854. TOKDEF(both),
  855. TOKDEF(bottom),
  856. TOKDEF(bottom_layer_sel),
  857. TOKDEF(boundary),
  858. TOKDEF(brickpat),
  859. TOKDEF(bundle),
  860. TOKDEF(bus),
  861. TOKDEF(bypass),
  862. TOKDEF(capacitance_resolution),
  863. TOKDEF(capacitor),
  864. TOKDEF(case_sensitive),
  865. TOKDEF(cct1),
  866. TOKDEF(cct1a),
  867. TOKDEF(center_center),
  868. TOKDEF(checking_trim_by_pin),
  869. TOKDEF(circ),
  870. TOKDEF(circle),
  871. TOKDEF(circuit),
  872. TOKDEF(class),
  873. TOKDEF(class_class),
  874. TOKDEF(classes),
  875. TOKDEF(clear),
  876. TOKDEF(clearance),
  877. TOKDEF(cluster),
  878. TOKDEF(cm),
  879. TOKDEF(color),
  880. TOKDEF(colors),
  881. TOKDEF(comment),
  882. TOKDEF(comp),
  883. TOKDEF(comp_edge_center),
  884. TOKDEF(comp_order),
  885. TOKDEF(component),
  886. TOKDEF(composite),
  887. TOKDEF(conductance_resolution),
  888. TOKDEF(conductor),
  889. TOKDEF(conflict),
  890. TOKDEF(connect),
  891. TOKDEF(constant),
  892. TOKDEF(contact),
  893. TOKDEF(control),
  894. TOKDEF(corner),
  895. TOKDEF(corners),
  896. TOKDEF(cost),
  897. TOKDEF(created_time),
  898. TOKDEF(cross),
  899. TOKDEF(crosstalk_model),
  900. TOKDEF(current_resolution),
  901. TOKDEF(delete_pins),
  902. TOKDEF(deleted),
  903. TOKDEF(deleted_keepout),
  904. TOKDEF(delta),
  905. TOKDEF(diagonal),
  906. TOKDEF(direction),
  907. TOKDEF(directory),
  908. TOKDEF(discrete),
  909. TOKDEF(effective_via_length),
  910. TOKDEF(elongate_keepout),
  911. TOKDEF(exclude),
  912. TOKDEF(expose),
  913. TOKDEF(extra_image_directory),
  914. TOKDEF(family),
  915. TOKDEF(family_family),
  916. TOKDEF(family_family_spacing),
  917. TOKDEF(fanout),
  918. TOKDEF(farad),
  919. TOKDEF(file),
  920. TOKDEF(fit),
  921. TOKDEF(fix),
  922. TOKDEF(flip_style),
  923. TOKDEF(floor_plan),
  924. TOKDEF(footprint),
  925. TOKDEF(forbidden),
  926. TOKDEF(force_to_terminal_point),
  927. TOKDEF(forgotten),
  928. TOKDEF(free),
  929. TOKDEF(fromto),
  930. TOKDEF(front),
  931. TOKDEF(front_only),
  932. TOKDEF(gap),
  933. TOKDEF(gate),
  934. TOKDEF(gates),
  935. TOKDEF(generated_by_freeroute),
  936. TOKDEF(global),
  937. TOKDEF(grid),
  938. TOKDEF(group),
  939. TOKDEF(group_set),
  940. TOKDEF(guide),
  941. TOKDEF(hard),
  942. TOKDEF(height),
  943. TOKDEF(high),
  944. TOKDEF(history),
  945. TOKDEF(horizontal),
  946. TOKDEF(host_cad),
  947. TOKDEF(host_version),
  948. TOKDEF(image),
  949. TOKDEF(image_conductor),
  950. TOKDEF(image_image),
  951. TOKDEF(image_image_spacing),
  952. TOKDEF(image_outline_clearance),
  953. TOKDEF(image_set),
  954. TOKDEF(image_type),
  955. TOKDEF(inch),
  956. TOKDEF(include),
  957. TOKDEF(include_pins_in_crosstalk),
  958. TOKDEF(inductance_resolution),
  959. TOKDEF(insert),
  960. TOKDEF(instcnfg),
  961. TOKDEF(inter_layer_clearance),
  962. TOKDEF(jumper),
  963. TOKDEF(junction_type),
  964. TOKDEF(keepout),
  965. TOKDEF(kg),
  966. TOKDEF(kohm),
  967. TOKDEF(large),
  968. TOKDEF(large_large),
  969. TOKDEF(layer),
  970. TOKDEF(layer_depth),
  971. TOKDEF(layer_noise_weight),
  972. TOKDEF(layer_pair),
  973. TOKDEF(layer_rule),
  974. TOKDEF(length),
  975. TOKDEF(length_amplitude),
  976. TOKDEF(length_factor),
  977. TOKDEF(length_gap),
  978. TOKDEF(library),
  979. TOKDEF(library_out),
  980. TOKDEF(limit),
  981. TOKDEF(limit_bends),
  982. TOKDEF(limit_crossing),
  983. TOKDEF(limit_vias),
  984. TOKDEF(limit_way),
  985. TOKDEF(linear),
  986. TOKDEF(linear_interpolation),
  987. TOKDEF(load),
  988. TOKDEF(lock_type),
  989. TOKDEF(logical_part),
  990. TOKDEF(logical_part_mapping),
  991. TOKDEF(low),
  992. TOKDEF(match_fromto_delay),
  993. TOKDEF(match_fromto_length),
  994. TOKDEF(match_group_delay),
  995. TOKDEF(match_group_length),
  996. TOKDEF(match_net_delay),
  997. TOKDEF(match_net_length),
  998. TOKDEF(max_delay),
  999. TOKDEF(max_len),
  1000. TOKDEF(max_length),
  1001. TOKDEF(max_noise),
  1002. TOKDEF(max_restricted_layer_length),
  1003. TOKDEF(max_stagger),
  1004. TOKDEF(max_stub),
  1005. TOKDEF(max_total_delay),
  1006. TOKDEF(max_total_length),
  1007. TOKDEF(max_total_vias),
  1008. TOKDEF(medium),
  1009. TOKDEF(mhenry),
  1010. TOKDEF(mho),
  1011. TOKDEF(microvia),
  1012. TOKDEF(mid_driven),
  1013. TOKDEF(mil),
  1014. TOKDEF(min_gap),
  1015. TOKDEF(mirror),
  1016. TOKDEF(mirror_first),
  1017. TOKDEF(mixed),
  1018. TOKDEF(mm),
  1019. TOKDEF(negative_diagonal),
  1020. TOKDEF(net),
  1021. TOKDEF(net_number),
  1022. TOKDEF(net_out),
  1023. TOKDEF(net_pin_changes),
  1024. TOKDEF(nets),
  1025. TOKDEF(network),
  1026. TOKDEF(network_out),
  1027. TOKDEF(no),
  1028. TOKDEF(noexpose),
  1029. TOKDEF(noise_accumulation),
  1030. TOKDEF(noise_calculation),
  1031. TOKDEF(normal),
  1032. TOKDEF(object_type),
  1033. TOKDEF(off),
  1034. TOKDEF(off_grid),
  1035. TOKDEF(offset),
  1036. TOKDEF(on),
  1037. TOKDEF(open),
  1038. TOKDEF(opposite_side),
  1039. TOKDEF(order),
  1040. TOKDEF(orthogonal),
  1041. TOKDEF(outline),
  1042. TOKDEF(overlap),
  1043. TOKDEF(pad),
  1044. TOKDEF(pad_pad),
  1045. TOKDEF(padstack),
  1046. TOKDEF(pair),
  1047. TOKDEF(parallel),
  1048. TOKDEF(parallel_noise),
  1049. TOKDEF(parallel_segment),
  1050. TOKDEF(parser),
  1051. TOKDEF(part_library),
  1052. TOKDEF(path),
  1053. TOKDEF(pcb),
  1054. TOKDEF(permit_orient),
  1055. TOKDEF(permit_side),
  1056. TOKDEF(physical),
  1057. TOKDEF(physical_part_mapping),
  1058. TOKDEF(piggyback),
  1059. TOKDEF(pin),
  1060. TOKDEF(pin_allow),
  1061. TOKDEF(pin_cap_via),
  1062. TOKDEF(pin_via_cap),
  1063. TOKDEF(pin_width_taper),
  1064. TOKDEF(pins),
  1065. TOKDEF(pintype),
  1066. TOKDEF(place),
  1067. TOKDEF(place_boundary),
  1068. TOKDEF(place_control),
  1069. TOKDEF(place_keepout),
  1070. TOKDEF(place_rule),
  1071. TOKDEF(placement),
  1072. TOKDEF(plan),
  1073. TOKDEF(plane),
  1074. TOKDEF(pn),
  1075. TOKDEF(point),
  1076. TOKDEF(polyline_path), // used by freerouting.com
  1077. TOKDEF(polygon),
  1078. TOKDEF(position),
  1079. TOKDEF(positive_diagonal),
  1080. TOKDEF(power),
  1081. TOKDEF(power_dissipation),
  1082. TOKDEF(power_fanout),
  1083. TOKDEF(prefix),
  1084. TOKDEF(primary),
  1085. TOKDEF(priority),
  1086. TOKDEF(property),
  1087. TOKDEF(protect),
  1088. TOKDEF(qarc),
  1089. TOKDEF(quarter),
  1090. TOKDEF(radius),
  1091. TOKDEF(ratio),
  1092. TOKDEF(ratio_tolerance),
  1093. TOKDEF(rect),
  1094. TOKDEF(reduced),
  1095. TOKDEF(region),
  1096. TOKDEF(region_class),
  1097. TOKDEF(region_class_class),
  1098. TOKDEF(region_net),
  1099. TOKDEF(relative_delay),
  1100. TOKDEF(relative_group_delay),
  1101. TOKDEF(relative_group_length),
  1102. TOKDEF(relative_length),
  1103. TOKDEF(reorder),
  1104. TOKDEF(reroute_order_viols),
  1105. TOKDEF(resistance_resolution),
  1106. TOKDEF(resistor),
  1107. TOKDEF(resolution),
  1108. TOKDEF(restricted_layer_length_factor),
  1109. TOKDEF(room),
  1110. TOKDEF(rotate),
  1111. TOKDEF(rotate_first),
  1112. TOKDEF(round),
  1113. TOKDEF(roundoff_rotation),
  1114. TOKDEF(route),
  1115. TOKDEF(route_to_fanout_only),
  1116. TOKDEF(routes),
  1117. TOKDEF(routes_include),
  1118. TOKDEF(rule),
  1119. TOKDEF(same_net_checking),
  1120. TOKDEF(sample_window),
  1121. TOKDEF(saturation_length),
  1122. TOKDEF(sec),
  1123. TOKDEF(secondary),
  1124. TOKDEF(self),
  1125. TOKDEF(sequence_number),
  1126. TOKDEF(session),
  1127. TOKDEF(set_color),
  1128. TOKDEF(set_pattern),
  1129. TOKDEF(shape),
  1130. TOKDEF(shield),
  1131. TOKDEF(shield_gap),
  1132. TOKDEF(shield_loop),
  1133. TOKDEF(shield_tie_down_interval),
  1134. TOKDEF(shield_width),
  1135. TOKDEF(side),
  1136. TOKDEF(signal),
  1137. TOKDEF(site),
  1138. TOKDEF(small),
  1139. TOKDEF(smd),
  1140. TOKDEF(snap),
  1141. TOKDEF(snap_angle),
  1142. TOKDEF(soft),
  1143. TOKDEF(source),
  1144. TOKDEF(space_in_quoted_tokens),
  1145. TOKDEF(spacing),
  1146. TOKDEF(spare),
  1147. TOKDEF(spiral_via),
  1148. TOKDEF(square),
  1149. TOKDEF(stack_via),
  1150. TOKDEF(stack_via_depth),
  1151. TOKDEF(standard),
  1152. TOKDEF(starburst),
  1153. TOKDEF(status),
  1154. TOKDEF(structure),
  1155. TOKDEF(structure_out),
  1156. TOKDEF(subgate),
  1157. TOKDEF(subgates),
  1158. TOKDEF(substituted),
  1159. TOKDEF(such),
  1160. TOKDEF(suffix),
  1161. TOKDEF(super_placement),
  1162. TOKDEF(supply),
  1163. TOKDEF(supply_pin),
  1164. TOKDEF(swapping),
  1165. TOKDEF(switch_window),
  1166. TOKDEF(system),
  1167. TOKDEF(tandem_noise),
  1168. TOKDEF(tandem_segment),
  1169. TOKDEF(tandem_shield_overhang),
  1170. TOKDEF(terminal),
  1171. TOKDEF(terminator),
  1172. TOKDEF(term_only),
  1173. TOKDEF(test),
  1174. TOKDEF(test_points),
  1175. TOKDEF(testpoint),
  1176. TOKDEF(threshold),
  1177. TOKDEF(time_length_factor),
  1178. TOKDEF(time_resolution),
  1179. TOKDEF(tjunction),
  1180. TOKDEF(tolerance),
  1181. TOKDEF(top),
  1182. TOKDEF(topology),
  1183. TOKDEF(total),
  1184. TOKDEF(track_id),
  1185. TOKDEF(turret),
  1186. TOKDEF(type),
  1187. TOKDEF(um),
  1188. TOKDEF(unassigned),
  1189. TOKDEF(unconnects),
  1190. TOKDEF(unit),
  1191. TOKDEF(up),
  1192. TOKDEF(use_array),
  1193. TOKDEF(use_layer),
  1194. TOKDEF(use_net),
  1195. TOKDEF(use_via),
  1196. TOKDEF(value),
  1197. TOKDEF(vertical),
  1198. TOKDEF(via),
  1199. TOKDEF(via_array_template),
  1200. TOKDEF(via_at_smd),
  1201. TOKDEF(via_keepout),
  1202. TOKDEF(via_number),
  1203. TOKDEF(via_rotate_first),
  1204. TOKDEF(via_site),
  1205. TOKDEF(via_size),
  1206. TOKDEF(virtual_pin),
  1207. TOKDEF(volt),
  1208. TOKDEF(voltage_resolution),
  1209. TOKDEF(was_is),
  1210. TOKDEF(way),
  1211. TOKDEF(weight),
  1212. TOKDEF(width),
  1213. TOKDEF(window),
  1214. TOKDEF(wire),
  1215. TOKDEF(wire_keepout),
  1216. TOKDEF(wires),
  1217. TOKDEF(wires_include),
  1218. TOKDEF(wiring),
  1219. TOKDEF(write_resolution),
  1220. TOKDEF(x),
  1221. TOKDEF(xy),
  1222. TOKDEF(y),
  1223. };
  1224. /* To run this test code, simply copy some DSN text to the clipboard, then run
  1225. the program from the command line and it will beautify the input from the
  1226. clipboard to stdout. stderr gets errors, if any.
  1227. The wxApp is involved because the clipboard is not available to a raw
  1228. int main() type program on all platforms.
  1229. */
  1230. class DSNTEST : public wxApp
  1231. {
  1232. DSNLEXER* lexer;
  1233. int nestLevel;
  1234. void recursion() throw( IOError );
  1235. void indent()
  1236. {
  1237. const int NESTWIDTH = 2;
  1238. printf("\n");
  1239. for( int i=0; i<nestLevel; ++i )
  1240. printf( "%*c", NESTWIDTH, ' ' );
  1241. }
  1242. public:
  1243. DSNTEST() :
  1244. lexer(0),
  1245. nestLevel(0)
  1246. {}
  1247. ~DSNTEST()
  1248. {
  1249. delete lexer;
  1250. }
  1251. virtual bool OnInit();
  1252. };
  1253. IMPLEMENT_APP( DSNTEST )
  1254. bool DSNTEST::OnInit()
  1255. {
  1256. #if 0 // file based LINE_READER.
  1257. wxFFile file;
  1258. wxString filename( wxT("/tmp/testdesigns/test.dsn") );
  1259. FILE* fp = wxFopen( filename, wxT("r") );
  1260. if( !fp )
  1261. {
  1262. fprintf( stderr, "unable to open file \"%s\"\n",
  1263. (const char*) filename.mb_str() );
  1264. exit(1);
  1265. }
  1266. file.Attach( fp ); // "exception safe" way to close the file.
  1267. // this won't compile without a token table.
  1268. DSNLEXER lexer( fp, filename, keywords, DIM(keywords) );
  1269. #else // clipboard based line reader
  1270. if( !wxTheClipboard->Open() )
  1271. {
  1272. fprintf( stderr, "unable to open clipboard\n" );
  1273. exit( 1 );
  1274. }
  1275. wxTextDataObject dataObj;
  1276. if( !wxTheClipboard->GetData( dataObj ) )
  1277. {
  1278. fprintf( stderr, "nothing of interest on clipboard\n" );
  1279. exit( 2 );
  1280. }
  1281. int formatCount = dataObj.GetFormatCount();
  1282. fprintf( stderr, "formatCount:%d\n", formatCount );
  1283. wxDataFormat* formats = new wxDataFormat[formatCount];
  1284. dataObj.GetAllFormats( formats );
  1285. for( int fmt=0; fmt<formatCount; ++fmt )
  1286. {
  1287. fprintf( stderr, "format:%d\n", formats[fmt].GetType() );
  1288. // @todo: what are these formats in terms of enum strings, and how
  1289. // do they vary across platforms. I am seeing
  1290. // on linux: 2 formats, 13 and 1
  1291. }
  1292. lexer = new DSNLEXER( std::string( CONV_TO_UTF8( dataObj.GetText() ) ),
  1293. keywords, DIM(keywords) );
  1294. #endif
  1295. // read the stream via the lexer, and use recursion to establish a nesting
  1296. // level and some output.
  1297. try
  1298. {
  1299. int tok;
  1300. while( (tok = lexer->NextTok()) != DSN_EOF )
  1301. {
  1302. if( tok == DSN_LEFT )
  1303. {
  1304. recursion();
  1305. }
  1306. else
  1307. printf( " %s", lexer->CurText() );
  1308. }
  1309. printf("\n");
  1310. }
  1311. catch( IOError ioe )
  1312. {
  1313. fprintf( stderr, "%s\n", CONV_TO_UTF8( ioe.errorText ) );
  1314. }
  1315. return 0;
  1316. }
  1317. void DSNTEST::recursion() throw(IOError)
  1318. {
  1319. int tok;
  1320. const char* space = "";
  1321. indent();
  1322. printf("(");
  1323. while( (tok = lexer->NextTok()) != DSN_EOF && tok != DSN_RIGHT )
  1324. {
  1325. if( tok == DSN_LEFT )
  1326. {
  1327. ++nestLevel;
  1328. recursion();
  1329. --nestLevel;
  1330. }
  1331. else
  1332. printf( "%s%s", space, lexer->CurText() );
  1333. space = " "; // only the first tok gets no leading space.
  1334. }
  1335. printf(")");
  1336. }
  1337. #endif