class Syntax::Fortran

A tokenizer for free-form Fortran source code.

def step

Step through a single iteration of the tokenization process.
def step
  case
  when check( /program\s+/ )
    start_group :keyword, scan( /program\s+/ )
    start_group :function,  scan_until( /(?=[;(\s]|#{EOL})/ )
  when check( /subroutine\s+/ )
    start_group :keyword, scan( /subroutine\s+/ )
    start_group :function,  scan_until( /(?=[;(\s]|#{EOL})/ )
  when check( /function\s+/ )
    start_group :keyword, scan( /function\s+/ )
    start_group :function,  scan_until( /(?=[;(\s]|#{EOL})/ )
  when check( /module\s+/ )
    start_group :keyword, scan( /module\s+/ )
    start_group :function,  scan_until( /(?=[;\s]|#{EOL})/ )
  when check( /\.true\.|\.false\.|\.TRUE\.|\.FALSE\./ )
    start_group :constant,
    scan(/\.true\.|\.false\.|\.TRUE\.|\.FALSE\./)
  when scan( /(\d+\.?\d*|\d*\.?\d+)([eEdDqQ][+-]?\d+)?(_\w+)?/ )
    start_group :number, matched
  when scan( /[bB]\'[01]+\'|[oO]\'[0-7]+\'|[zZ]\'[0-9a-fA-F]+\'/ )
    start_group :number, matched
  else
    case peek(1)
    when /[\n\r]/
      start_group :normal, scan( /\s+/ )
    when /\s/
      start_group :normal, scan( /\s+/ )
    when "!"
      start_group :comment, scan( /![^\n\r]*/ )
    when /[A-Za-z]/
      word = scan( /\w+/ )
      if KEYWORDS.include?(word)
        start_group :keyword, word
      elsif TYPES.include?(word)
        start_group :type, word
      elsif INTRINSICS.include?(word)
        start_group :function, word
      elsif
        start_group :ident, word
      end
    when '"'
      # allow for continuation characters within strings
      start_group :string, scan(/"([^"]*(&[ ]*[\n\r]+)?)*"/)
    when "'"
      # allow for continuation characters within strings
      start_group :string, scan(/'([^']*(&[ ]*[\n\r]+)?)*'/)
    when /[-!?*\/+=<>()\[\]\{}:;,&|%]/
      start_group :punct, scan(/./)
    else
      # pass everything else through
      append getch
    end
  end
end