home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / eco12.zip / eco.e next >
Text File  |  1999-06-28  |  14KB  |  460 lines

  1. /* 
  2.   This is eco.e, version 1.2, the source code of the 
  3.   EPM module eco.ex for compiling Java, C and IPF.
  4.   
  5.   Copyright (c) 1998-1999 Walter Schmidt
  6. */
  7.  
  8.  
  9. compile if EVERSION < '6.03b'
  10.   *** Error: EPM version 6.03b required
  11. compile endif
  12.  
  13.  
  14. compile if not defined(SMALL) -- being compiled separately
  15.   const ECO_PRO = 0           -- linkable module
  16. compile else                  
  17.   const ECO_PRO = 1           -- built-in "Pro" version
  18. compile endif
  19.  
  20.  
  21. compile if not defined(SMALL) -- being compiled separately
  22.   include 'stdconst.e'
  23.   include 'colors.e'
  24.   define INCLUDING_FILE = 'eco.e'
  25.   tryinclude 'MYCNF.E'
  26.   compile if not defined(SITE_CONFIG)
  27.     const SITE_CONFIG = 'SITECNF.E'
  28.   compile endif
  29.   compile if SITE_CONFIG
  30.     tryinclude SITE_CONFIG
  31.   compile endif
  32.   compile if not defined(NLS_LANGUAGE)
  33.     const NLS_LANGUAGE = 'ENGLISH'
  34.   compile endif
  35. compile endif -- SMALL
  36.  
  37.  
  38. define EcoPrimVersion = '1.2'
  39. const
  40. compile if ECO_PRO
  41.   ECO_VERSION = EcoPrimVersion || ' Pro'
  42. compile else
  43.   ECO_VERSION = EcoPrimVersion
  44. compile endif
  45.  
  46. compile if not defined(ECO_EXTENSIONS)
  47.   const
  48.      ECO_EXTENSIONS = 'C H SQC CPP HPP CXX HXX SQX JAV JAVA IPF' 
  49.      -- keep in sync with CKEYS.E and TAGS.E
  50. compile endif
  51.  
  52. const
  53.    ECO_MENU_ID   = 21
  54.    ECO_MENU_C_ID = 2101
  55.    ECO_MENU_V_ID = 2118
  56.    ECO_MENU_L_ID = 2102
  57.    ECO_MENU_H_ID = 2103
  58.    ECO_MENU_S_ID = 2104
  59.    ECO_MENU_A_ID = 2105
  60.  
  61.    ECO_COPYRIGHT_STRING = '(C) Walter Schmidt 1998-1999'
  62.    ECO_MENU__MSG = 'Menus related to Java and C programming'
  63.    ECO_COM__MSG  = 'Execute a compiler in the directory of this file'
  64.    ECO_VIEW__MSG = 'View transcript of compilation'
  65.    ECO_ERR__MSG  = 'Show next error in source file'
  66.    ECO_TSH__MSG  = 'Toggle syntax highlighting of this file'
  67.    
  68.    ECO_NOSOURCE  = 'This is not a real file'
  69.    ECO_NOLOG     = 'There is no transcript'
  70.    ECO_NOERR     = 'No more errors'
  71.    ECO_NOHILIT   = 'ECO supports highlighting for Java, HTML, C, IPF and Makefiles only'
  72.  
  73.    ECO_APP       = 'ECO'
  74.    ECO_DEFCMD    = 'javac %**F'
  75.    
  76.    ECO_WTITLE    = 'Compiler invocation:'
  77.    ECO_PROMPTTEXT= '( %**F and %**N will expand to the current '||
  78.                    'filename with/without extension. )'
  79.  
  80. compile if not defined(HELP_PROD_MENUP__MSG)
  81.   const HELP_PROD_MENUP__MSG=    \1'Copyright and version info'
  82. compile endif
  83.  
  84.  
  85. definit
  86.    universal app_hini
  87. -- initialize compiler command, if not yet defined:
  88.    if queryprofile(app_hini, ECO_APP, 'COMMAND') = '' then
  89.       call setprofile(app_hini, ECO_APP, 'COMMAND', ECO_DEFCMD)
  90.    endif
  91. -- add menu:   
  92.    'PostMe BuildCompMenu'
  93.  
  94.  
  95. compile if ECO_PRO
  96.   defselect -- called at startup time and when switching files
  97.       -- enable the Compile menu if supported language file loaded
  98.       'PostMe EcoEnableCompMenu'
  99.   
  100.   defload -- called when loading files
  101.       -- enable the Compile menu if supported language file loaded
  102.       'PostMe EcoEnableCompMenu'
  103. compile endif
  104.  
  105.  
  106. defexit
  107.    universal defaultmenu
  108.    deletemenu defaultmenu,ECO_MENU_ID,0,0
  109.    call maybe_show_menu()
  110.  
  111.  
  112. defc BuildCompMenu
  113.    universal  defaultmenu
  114. -- Temporarily delete Help menu so we can put it back later on the end 
  115. -- of the menu bar where it belongs!
  116.    deletemenu defaultmenu, HELP_MENU_ID, 0, 0  -- delete the existing Help menu
  117.                                                -- (we want it to stay at the right)
  118.    BUILDSUBMENU defaultmenu, ECO_MENU_ID, 'Comp~ile', \1ECO_MENU__MSG, 0,0
  119.    BUILDMENUITEM defaultmenu, ECO_MENU_ID, ECO_MENU_C_ID, '~Compile...',
  120.                               'eco_compile'\1ECO_COM__MSG, 0,0
  121.    BUILDMENUITEM defaultmenu, ECO_MENU_ID, ECO_MENU_V_ID, '~View transcript',
  122.                               'eco_view_result'\1ECO_VIEW__MSG, 0,0
  123.    BUILDMENUITEM defaultmenu, ECO_MENU_ID, ECO_MENU_L_ID, '~Locate next error',
  124.                               'eco_next_error'\1ECO_ERR__MSG, 0,0
  125.    BUILDMENUITEM defaultmenu, ECO_MENU_ID, ECO_MENU_H_ID, '~Highlighting',
  126.                               'eco_toggle_hilite'\1ECO_TSH__MSG, 0,0
  127.    BUILDMENUITEM defaultmenu, ECO_MENU_ID, ECO_MENU_S_ID, \0, '', 4, 0
  128.    BUILDMENUITEM defaultmenu, ECO_MENU_ID, ECO_MENU_A_ID, '~About...', 'eco_ver' HELP_PROD_MENUP__MSG,0,0
  129.  
  130.    call readd_help_menu();   -- Put Help menu back in
  131.    showmenu defaultmenu
  132. compile if ECO_PRO   
  133.    'Postme EcoEnableCompMenu'
  134. compile endif
  135.  
  136. -- enable the ECO menu only if the current file is an ECO-file, i. e. a 
  137. -- file with one of the extensions defined by ECO_EXTENSIONS. Also enabled:
  138. -- command shells for the support of finding errors, and HTML files.
  139. -- Only defined with built-in ECO, not with linkable module!
  140.  
  141. compile if ECO_PRO
  142.   DEFC EcoEnableCompMenu =
  143.      ext = FILETYPE()
  144.      eco_enabled = WORDPOS(ext, ECO_EXTENSIONS ||' HTM HTML MAK') | 
  145.         POS("COMMAND_SHELL", ext) | POS("MAKEFILE", UPCASE(.filename))
  146.      eco_comp_enabled = WORDPOS(EXT, ECO_EXTENSIONS) | POS("MAKEFILE", UPCASE(.filename))
  147.      eco_hlit_enabled = WORDPOS(EXT, ECO_EXTENSIONS||' HTM HTML MAK') | 
  148.         POS("MAKEFILE", UPCASE(.filename))
  149.      SetMenuAttribute(ECO_MENU_ID, 16384, eco_enabled)
  150.      if eco_enabled then
  151.         SetMenuAttribute(ECO_MENU_C_ID, 16384, eco_comp_enabled)
  152.         SetMenuAttribute(ECO_MENU_H_ID, 16384, eco_hlit_enabled)
  153.      endif
  154.   RETURN
  155. compile endif
  156.  
  157. defc eco_compile
  158.    universal eco_id, eco_shellid, eco_first_line, eco_current_line,
  159.              eco_dr, eco_dir, app_hini
  160.    call eco_parse_filename(.filename, eco_dr, eco_dir, stem, ext)
  161.    if (eco_dr = '') or (eco_dir ='') then
  162.       sayerror ECO_NOSOURCE
  163.       return
  164.    endif
  165.    current_cmd = queryprofile(app_hini, ECO_APP, 'COMMAND')
  166.    sbuf = entrybox(ECO_WTITLE, 
  167.                    '/~Run/~Cancel/',
  168.                    current_cmd,254,80,
  169.                    atoi(0) ||            -- default button # 
  170.                    atoi(0) ||            -- help panel ID (0 for no help)
  171.                    gethwnd(APP_HANDLE) ||
  172.                    ECO_PROMPTTEXT||\0) 
  173.    button = asc(leftstr(sbuf,1))
  174.    if button=1 then 
  175.       EOS = pos(\0,sbuf,2)    -- CHR(0) signifies End Of String
  176.       cmd = substr(sbuf,2,EOS-2)
  177.    else
  178.       cmd = '' -- cancelled or closed otherwise
  179.    endif
  180.    if cmd <> '' then
  181.       call setprofile(app_hini, ECO_APP, 'COMMAND', cmd)
  182.       call eco_save()
  183.       call eco_cd()
  184.       display -2
  185.       rc = 0
  186.       activatefile eco_id
  187.       display 2
  188.       if rc<>0 then
  189.          -- activatefile failed -> invalid fileid
  190.          'shell'
  191.          .AUTOSAVE = 0
  192.          getfileid eco_id
  193.          eco_shellid = substr(.filename,16)
  194.       else
  195.          .line = .last
  196.       endif
  197.       command = eco_makecmd(cmd, stem, ext)
  198.       'shell_write' eco_shellid eco_dr 
  199.       'shell_write' eco_shellid "cd" eco_dir
  200.       .line = .last
  201.       eco_first_line = .line
  202.       eco_current_line = .line
  203.       'shell_write' eco_shellid command
  204.    endif
  205.  
  206.  
  207. defc eco_View_result
  208.    universal eco_id, eco_current_line, eco_first_line
  209.    getfileid id
  210.    if id = eco_id then
  211.       .line = eco_first_line
  212.    else
  213.       display -2
  214.       rc = 0
  215.       activatefile eco_id
  216.       display 2
  217.       if rc<>0 then
  218.          sayerror ECO_NOLOG
  219.       else
  220.          .line = eco_current_line
  221.       endif
  222.    endif
  223.  
  224.  
  225. defc eco_next_error
  226.    universal eco_id, eco_current_line, eco_first_line, eco_dr, eco_dir,
  227.              eco_mark_end                       -- circled region starts
  228.                                                 -- in .line at .col, ends at
  229.                                                 -- eco_mark_end
  230.    getfileid id
  231.    if id = eco_id then
  232.       if .line = .last then
  233.          current_line = eco_first_line + 1
  234.       else
  235.          current_line = .line -- the current error, not the next one
  236.       endif
  237.    else
  238.       display -2
  239.       rc = 0
  240.       activatefile eco_id
  241.       display 2
  242.       if rc<>0 then
  243.          sayerror ECO_NOLOG
  244.          activatefile id
  245.          return
  246.       endif
  247.       current_line = eco_current_line + 1
  248.    endif
  249.  
  250.    call directory(eco_dr||eco_dir)
  251.    eco_last_line = .last
  252.    found = 0
  253.  
  254.    ErrorColEnd = 0
  255.    do while (current_line < eco_last_line)
  256.       getline errorline, current_line
  257.       -- try VisualAge format
  258.       parse value errorline with Filename '(' LineNum ':' ErrorCol ') :' ErrorNr ':' ErrorMsg  
  259.       if (FileName <> '') & isnum(LineNum) & isnum(ErrorCol) & (ErrorMsg <> '') then
  260.          -- Filename & LineNum & ErrorCol & ErrorMsg recognized
  261.          found = 1
  262.          leave
  263.       endif
  264.       -- try Jikes/emacs format
  265.       parse value errorline with Filename ':' LineNum ':' ErrorCol ':' LineNum ':' ErrorColEnd ': ' ErrorMsg  
  266.       if (FileName <> '') & isnum(LineNum) & isnum(ErrorCol) & isnum(ErrorColEnd) & (ErrorMsg <> '') then
  267.          -- Filename & LineNum & ErrorCol & ErrorColEnd & ErrorMsg recognized
  268.          found = 1
  269.          leave
  270.       endif
  271.       -- try GNU/Java format
  272.       if substr(errorline, 2, 1) = ':' then
  273.          col = pos(':', errorline, 3)
  274.          if col then
  275.             Filename = leftstr(errorline, col-1)
  276.             parse value substr(errorline, col+1) with LineNum ':' ErrorMsg
  277.          else
  278.             Filename = '' 
  279.             LineNum = ''
  280.             ErrorMsg = ''
  281.          endif
  282.       else
  283.          parse value errorline with Filename ':' LineNum ':'  ErrorMsg
  284.       endif
  285.       if (FileName <> '') & isnum(LineNum) & (ErrorMsg <> '') then 
  286.          -- FileName & Linenum & ErrorMsg recognized,
  287.          -- now try to find ErrorCol, too:
  288.          if (current_line+2 <= .last) then
  289.             getline errorline, current_line+2
  290.             ErrorCol = pos('^', errorline)
  291.          else
  292.             ErrorCol=0
  293.          endif
  294.          found = 1
  295.          leave
  296.       endif
  297.       --try IPFC format
  298.       parse value errorline with '<' Filename ':' LineNum '>'  ErrorMsg
  299.       if  (FileName <> '') & isnum(LineNum) & (ErrorMsg <> '') then
  300.          -- FileName & Linenum & ErrorMsg recognized;
  301.          -- ErrorCol can, however, not be determined easily.
  302.          ErrorCol = 0
  303.          found = 1
  304.          leave
  305.       endif
  306.       current_line = current_line + 1
  307.    enddo
  308.  
  309.    if found then
  310.       eco_current_line = current_line 
  311.       FileName = translate(FileName,'\','/')
  312.       'edit' FileName
  313.       .line = LineNum
  314.       sayerror ErrorMsg
  315.       if ErrorCol>0 then
  316.          .col = ErrorCol                        -- position cursor to the 
  317.                                                 -- begin of the erroneous
  318.                                                 -- token
  319.          getline sourceline, LineNum
  320.          if ErrorColEnd = 0 then
  321.              do i = ErrorCol to length(sourceline)  -- approximate error by 
  322.                 c = substr(sourceline, i, 1)    -- a variable at current
  323.                 if ( pos(translate(c),          -- position
  324.                       "ABCDEFGHIJKLMNOPQRSTUVWXYZ_1234567890") = 0 ) then
  325.                    leave
  326.                 endif
  327.              end 
  328.              if i > ErrorCol then               -- variables with at least
  329.                 eco_mark_end = i - 1            -- one character
  330.              else                               -- other token
  331.                 eco_mark_end = ErrorCol
  332.              endif
  333.          else
  334.             eco_mark_end = ErrorColEnd
  335.          endif
  336.          'PostMe eco_circleit'
  337.       endif
  338.    else
  339.       activatefile id
  340.       sayerror ECO_NOERR
  341.    endif
  342.  
  343.  
  344. defproc eco_makecmd(command, fname, ext)
  345. -- we perform the following substitutions:
  346. -- %**N   ->  filename without extension
  347. -- %**F   ->  filename with extension
  348.  
  349.     do forever
  350.        posi=pos('%**N', command)
  351.        if posi then
  352.           command=delstr(command, posi, 4)
  353.           command = insertstr(fname, command, (posi-1))
  354.        else
  355.           leave
  356.        endif
  357.     enddo
  358.     do forever
  359.        posi=pos('%**F', command)
  360.        if posi then
  361.           command=delstr(command, posi, 4)
  362.           command = insertstr(fname'.'ext, command, (posi-1))
  363.        else
  364.           leave
  365.        endif
  366.     enddo
  367. return command
  368.  
  369.  
  370. defproc eco_parse_filename(qname, var dr, var dir, var stem, var ext)
  371.     n = lastpos('\',qname)
  372.     tmp = substr(qname,n+1)
  373.     p = lastpos('.', tmp)
  374.     if p <= 1   then
  375.        stem = tmp
  376.        ext = ''
  377.     else
  378.        stem = substr(tmp,1,p-1)
  379.        ext = substr(tmp,p+1)
  380.     endif
  381.     tmp = substr(qname,1,n)
  382.     n = pos(':',tmp)
  383.     dr = substr(tmp,1,n)
  384.     dir = substr(tmp,n+1)
  385.     if dir <> '\' then
  386.        dir = strip(dir,'T','\')
  387.     endif
  388. return
  389.  
  390.  
  391. defproc eco_cd
  392.    dirpos=lastpos('\',.filename)
  393.    if dirpos>1 then
  394.       call directory(substr(.filename,1,dirpos-1))
  395.    endif
  396. return
  397.  
  398.  
  399. defproc eco_save
  400.    if .modify then
  401.       'save'
  402.    endif
  403.    GETFILEID StartFileID
  404.    LOOP
  405.       NEXTFILE
  406.       GETFILEID NextFileID
  407.       IF NextFileID = StartFileID THEN
  408.          LEAVE
  409.       ENDIF
  410.       IF wordpos(FILETYPE(), ECO_EXTENSIONS) THEN
  411.          IF .modify THEN
  412.             'save'
  413.          ENDIF
  414.       ENDIF
  415.    ENDLOOP
  416.    ACTIVATEFILE StartFileID
  417. return
  418.  
  419.  
  420. defc eco_toggle_hilite
  421.    color_file=''
  422.    ext=filetype()
  423.    if wordpos(ext, 'JAV JAVA') then
  424.       color_file= 'EPMKWDS.JAV'
  425.    elseif wordpos(ext, 'HTM HTML') then
  426.       color_file= 'EPMKWDS.HTM'
  427.    elseif ext = 'IPF' then
  428.       color_file= 'EPMKWDS.IPF'
  429.    elseif wordpos(ext, ECO_EXTENSIONS) then
  430.       color_file= 'EPMKWDS.C'
  431.    elseif (pos('MAKEFILE', upcase(.filename)) | ext='MAK') then
  432.       color_file='EPMKWDS.MAK'
  433.    else
  434.       beep(440,125)
  435.       sayerror ECO_NOHILIT
  436.       return
  437.    endif
  438.    current_toggle = windowmessage(1,  getpminfo(EPMINFO_EDITFRAME),
  439.                                   5505,          -- EPM_EDIT_KW_QUERYPARSE
  440.                                   0,
  441.                                   0)
  442.    'toggle_parse' (not current_toggle) color_file
  443. return
  444.  
  445.  
  446. defc eco_circleit
  447.    universal eco_mark_end
  448.    circleit 1, .line, .col, eco_mark_end, LIGHT_RED
  449. return
  450.  
  451.  
  452. defc eco_ver
  453.    call WinMessageBox('About the EPM Compiler Front End',
  454.                       'ECO version '||ECO_VERSION\13||
  455.                       ECO_COPYRIGHT_STRING,
  456.                       MB_OK+MB_INFORMATION+MB_DEFBUTTON1+MB_MOVEABLE)
  457.  
  458. /* finis */
  459.  
  460.