Module: Peruby::Lexer::Keywords

Defined in:
lib/peruby/lexer/keywords.rb

Overview

Perl keywords grouped by their grammar role.

Constant Summary collapse

RESERVED =
%w[
  BEGIN CHECK END INIT UNITCHECK and break cmp continue CORE default do else elsif eq eval evalbytes for foreach
  format ge given goto gt if last le local lt m my ne next no or our package q qq qr qw qx redo require return s
  state sub tr unless until use when while xor y
].to_h { |word| [word, word.upcase.to_sym] }.freeze
NAMED_UNARY =
%w[
  abs chdir chomp chop chr chroot cos defined delete each eof eval exists exp fc fileno getc gethostbyname
  getnetbyname getpgrp getprotobyname getprotobynumber getpwnam getpwuid getgrnam getgrgid hex int keys lc lcfirst
  length localtime
  lock log lstat oct ord quotemeta rand readlink ref reverse rewinddir scalar sin sqrt stat study uc ucfirst umask
  undef values write
].freeze
LISTOP =
%w[
  accept alarm all any atan2 bind binmode bless caller chmod chown close closedir connect crypt dbmclose dbmopen
  die dump exec exit fcntl first flock fork formline gethostbyaddr getnetbyaddr getpeername getpriority
  getservbyname getservbyport getsockname getsockopt glob gmtime grep import index ioctl join kill link listen
  map mkdir msgctl msgget msgrcv msgsnd none open opendir pack pipe pop pos print printf prototype push read
  readdir reduce
  readline readpipe recv rename reset rindex rmdir say seek seekdir select semctl semget semop send setpgrp
  setpriority
  setsockopt shift shmctl shmget shmread shmwrite shutdown sleep socket socketpair sort splice split sprintf srand
  substr symlink syscall sysopen sysread sysseek system syswrite tell telldir tie tied time truncate unlink
  unpack unshift untie utime vec wait waitpid wantarray warn
].freeze
FUNC0 =
%w[
  caller endgrent endhostent endnetent endprotoent endpwent endservent eof getgrent gethostent getlogin getnetent
  getprotoent getppid getpwent getservent setgrent sethostent setnetent setprotoent setpwent setservent time times
  wantarray
].freeze
SPECIAL_VARIABLES =
['_', '@', '!', '/', '\\', ',', '"', ';', '.', '0', '?', '$', '|', '^W', '^O', ']', '[',
'+', '-', '`', "'", '&'].freeze

Class Method Summary collapse

Class Method Details

.operator_word(word) ⇒ Object



57
58
59
60
# File 'lib/peruby/lexer/keywords.rb', line 57

def operator_word(word)
  { 'x' => :REPEAT, 'lt' => :SLT, 'gt' => :SGT, 'le' => :SLE, 'ge' => :SGE,
    'eq' => :SEQ, 'ne' => :SNE, 'cmp' => :SCMP, 'and' => :AND, 'or' => :OR, 'xor' => :XOR }[word]
end

.type(word, expect) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/peruby/lexer/keywords.rb', line 44

def type(word, expect)
  return :CONSTANT if word == 'constant'
  return :NOT if word == 'not'
  return operator_word(word) if expect == :operator && operator_word(word)
  return word.upcase.to_sym if %w[print printf say sort map grep].include?(word)
  return RESERVED[word] if RESERVED.key?(word)
  return :NAMED_UNARY if NAMED_UNARY.include?(word)
  return :LISTOP if LISTOP.include?(word)
  return :FUNC if FUNC0.include?(word)

  nil
end