cvecs ← {nv} ##.tokens cvec                 ⍝ Lex of APL src line.

Splits  an  APL  "source"  line  into  its constituent tokens. Character literal
strings, comments and expanses of "white space" are deemed single tokens.

Optional  left  argument [nv] deterimines whether numeric vectors (1 2 3) are to
be considered:

    0 multiple tokens (default)
    1 a single token

An alternative coding of the function might be:

    tokens2←{⎕ML←0                              ⍝ Lex of APL source line.
        alph←⎕A,⎕Á,'_∆⍙',26↑17↓⎕AV              ⍝ Alphabet for names
        all←{+/^\⍺∊⍵}                           ⍝ ∇ No of leading ⍺∊⍵
        acc←{n←⍺⍺ ⍵ ⋄ (⍺,⊂n↑⍵)lex n↓⍵}          ⍝ ∇∇ Accumulated tokens
        lex←{                                   ⍝ ∇ Next lexical token
            0=⍴⍵:⍺ ⋄ hd←⊃⍵                      ⍝ Next char else finished
            hd=' ':⍺{⍵ all' '}acc ⍵             ⍝ White Space
            hd∊alph:⍺{⍵ all alph,⎕D}acc ⍵       ⍝ Name
            hd∊'⎕:':⍺{1+(1↓⍵)all alph,':'}acc ⍵ ⍝ System Name / Keyword / guard
            hd='''':⍺{+/^\{⍵∨¯1⌽⍵}≠\⍵=hd}acc ⍵  ⍝ Char literal
            hd∊⎕D,'¯':⍺{⍵ all ⎕D,'.¯E'}acc ⍵    ⍝ Numeric scalar literal
            hd∊'⍺⍵∇':⍺{⍵ all hd}acc ⍵           ⍝ Meta character.
            hd='⍝':⍺⍴acc ⍵                      ⍝ Comment
            ⍺{1}acc ⍵                           ⍝ Single char token
        }
        (0⍴⊂'')lex,⍵
    }

Example:

      {⎕←disp tokens ⍵}¨⎕nr'tokens'
┌→─────┬─┬─┬───┬─┬─┬───────────────────────────┬──────────────────────┐ 
│tokens│←│{│⎕ML│←│3│                           │⍝ Lex of APL src line.│
└─────→┴→┴→┴──→┴→┴→┴──────────────────────────→┴─────────────────────→┘ 
┌⊖┐
│ │
└⊖┘
┌→───┬────┬─┬──┬─┬──┬─┬─────┬─┬──┬─┬──┬─┬───┬──────────┬─────────────────────┐ 
│    │alph│←│⎕A│,│⎕Á│,│'_∆⍙'│,│26│↑│17│↓│⎕AV│          │⍝ Alphabet for names.│
└───→┴───→┴→┴─→┴→┴─→┴→┴────→┴→┴─→┴→┴─→┴→┴──→┴─────────→┴────────────────────→┘ 
┌⊖┐
│ │
└⊖┘
┌→───┬───┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬───────────────────────┬─────────────────────┐ 
│    │all│←│{│+│/│^│\│⍺│∊│⍵│}│                       │⍝ No. of leading ⍺∊⍵.│
└───→┴──→┴→┴→┴→┴→┴→┴→┴→┴→┴→┴→┴──────────────────────→┴────────────────────→┘ 
┌⊖┐
│ │
└⊖┘
┌→───┬───┬─┬─┬─┬─┬─┬─┬─┬─┬─┬───┬─┬─┬─┬─┬─┬────────────────┬────────────────────┐ 
│    │acc│←│{│(│⍺│,│↑│/│⍵│)│lex│⊃│↓│/│⍵│}│                │⍝ Accumulate tokens.│
└───→┴──→┴→┴→┴→┴→┴→┴→┴→┴→┴→┴──→┴→┴→┴→┴→┴→┴───────────────→┴───────────────────→┘ 
┌⊖┐
│ │
└⊖┘
┌→───┬───┬─┬─┐ 
│    │lex│←│{│
└───→┴──→┴→┴→┘ 
┌⊖┐
│ │
└⊖┘
┌→───────┬─┬─┬─┬─┬─┬─┬─┬─┬─┬──┬─┬─┬─┬──────────────────┬──────────────────────────┐ 
│        │0│=│⍴│⍵│:│⍺│ │⋄│ │hd│←│↑│⍵│                  │⍝ Next char else finished.│
└───────→┴→┴→┴→┴→┴→┴→┴→┴→┴→┴─→┴→┴→┴→┴─────────────────→┴─────────────────────────→┘ 
┌⊖┐
│ │
└⊖┘
┌→───────┬──┬─┬───┬─┬─┬─┬───────────────────────┬──────────────┐ 
│        │hd│=│' '│:│⍺│{│                       │⍝ White Space.│
└───────→┴─→┴→┴──→┴→┴→┴→┴──────────────────────→┴─────────────→┘ 
┌→───────────┬────┬─┬─┬─┬───┬───┐ 
│            │size│←│⍵│ │all│' '│
└───────────→┴───→┴→┴→┴→┴──→┴──→┘ 
┌→───────────┬─┬─┬───┬─┬────┬─┬─┐ 
│            │⍺│ │acc│ │size│ │⍵│
└───────────→┴→┴→┴──→┴→┴───→┴→┴→┘ 
┌→───────┬─┬─┐ 
│        │}│⍵│
└───────→┴→┴→┘ 
┌⊖┐
│ │
└⊖┘
┌→───────┬──┬─┬────┬─┬─┬─┬──────────────────────┬──────┐ 
│        │hd│∊│alph│:│⍺│{│                      │⍝ Name│
└───────→┴─→┴→┴───→┴→┴→┴→┴─────────────────────→┴─────→┘ 
┌→───────────┬────┬─┬─┬─┬───┬─┬────┬─┬──┐ 
│            │size│←│⍵│ │all│ │alph│,│⎕D│
└───────────→┴───→┴→┴→┴→┴──→┴→┴───→┴→┴─→┘ 
┌→───────────┬─┬─┬───┬─┬────┬─┬─┐ 
│            │⍺│ │acc│ │size│ │⍵│
└───────────→┴→┴→┴──→┴→┴───→┴→┴→┘ 
┌→───────┬─┬─┐ 
│        │}│⍵│
└───────→┴→┴→┘ 
┌⊖┐
│ │
└⊖┘
┌→───────┬──┬─┬────┬─┬─┬─┬──────────────────────┬───────────────────────┐ 
│        │hd│∊│'⎕:'│:│⍺│{│                      │⍝ System Name / Keyword│
└───────→┴─→┴→┴───→┴→┴→┴→┴─────────────────────→┴──────────────────────→┘ 
┌→───────────┬────┬─┬─┬─┬───┬─┬──┬─┬────┐ 
│            │size│←│⍵│ │all│ │hd│,│alph│
└───────────→┴───→┴→┴→┴→┴──→┴→┴─→┴→┴───→┘ 
┌→───────────┬─┬─┬───┬─┬────┬─┬─┐ 
│            │⍺│ │acc│ │size│ │⍵│
└───────────→┴→┴→┴──→┴→┴───→┴→┴→┘ 
┌→───────┬─┬─┐ 
│        │}│⍵│
└───────→┴→┴→┘ 
┌⊖┐
│ │
└⊖┘
┌→───────┬──┬─┬────┬─┬─┬─┬──────────────────────┬──────────────┐ 
│        │hd│=│''''│:│⍺│{│                      │⍝ Char literal│
└───────→┴─→┴→┴───→┴→┴→┴→┴─────────────────────→┴─────────────→┘ 
┌→───────────┬────┬─┬─┬─┬─┬─┬─┬─┬─┬──┬─┬─┬─┬─┬─┬──┬─┬─┐ 
│            │size│←│+│/│^│\│{│⍵│∨│¯1│⌽│⍵│}│≠│\│hd│=│⍵│
└───────────→┴───→┴→┴→┴→┴→┴→┴→┴→┴→┴─→┴→┴→┴→┴→┴→┴─→┴→┴→┘ 
┌→───────────┬─┬─┬───┬─┬────┬─┬─┐ 
│            │⍺│ │acc│ │size│ │⍵│
└───────────→┴→┴→┴──→┴→┴───→┴→┴→┘ 
┌→───────┬─┬─┐ 
│        │}│⍵│
└───────→┴→┴→┘ 
┌⊖┐
│ │
└⊖┘
┌→───────┬──┬─┬──┬─┬───┬─┬─┬─┬────────────────────┬─────────────────┐ 
│        │hd│∊│⎕D│,│'¯'│:│⍺│{│                    │⍝ Numeric literal│
└───────→┴─→┴→┴─→┴→┴──→┴→┴→┴→┴───────────────────→┴────────────────→┘ 
┌→───────────┬────┬─┬─┬─┬───┬─┬──┬─┬─────┐ 
│            │size│←│⍵│ │all│ │⎕D│,│'.¯E'│
└───────────→┴───→┴→┴→┴→┴──→┴→┴─→┴→┴────→┘ 
┌→───────────┬─┬─┬───┬─┬────┬─┬─┐ 
│            │⍺│ │acc│ │size│ │⍵│
└───────────→┴→┴→┴──→┴→┴───→┴→┴→┘ 
┌→───────┬─┬─┐ 
│        │}│⍵│
└───────→┴→┴→┘ 
┌⊖┐
│ │
└⊖┘
┌→───────┬──┬─┬─────┬─┬─┬─┬─────────────────────┬────────────────┐ 
│        │hd│∊│'⍺⍵∇'│:│⍺│{│                     │⍝ ⍺⍺ or ⍵⍵ or ∇∇│
└───────→┴─→┴→┴────→┴→┴→┴→┴────────────────────→┴───────────────→┘ 
┌→───────────┬────┬─┬─┬─┬───┬─┬──┐ 
│            │size│←│⍵│ │all│ │hd│
└───────────→┴───→┴→┴→┴→┴──→┴→┴─→┘ 
┌→───────────┬─┬─┬───┬─┬────┬─┬─┐ 
│            │⍺│ │acc│ │size│ │⍵│
└───────────→┴→┴→┴──→┴→┴───→┴→┴→┘ 
┌→───────┬─┬─┐ 
│        │}│⍵│
└───────→┴→┴→┘ 
┌⊖┐
│ │
└⊖┘
┌→───────┬──┬─┬───┬─┬─┬─┬───┬─┬─┬─┬─┬─┬───────────────┬─────────┐ 
│        │hd│=│'⍝'│:│⍺│ │acc│(│⍴│⍵│)│⍵│               │⍝ Comment│
└───────→┴─→┴→┴──→┴→┴→┴→┴──→┴→┴→┴→┴→┴→┴──────────────→┴────────→┘ 
┌⊖┐
│ │
└⊖┘
┌→───────┬─┬─┬───┬─┬─┬─┬─┬───────────────────────┬────────────────────┐ 
│        │⍺│ │acc│ │1│ │⍵│                       │⍝ Single char token.│
└───────→┴→┴→┴──→┴→┴→┴→┴→┴──────────────────────→┴───────────────────→┘ 
┌→───┬─┐ 
│    │}│
└───→┴→┘ 
┌⊖┐
│ │
└⊖┘
┌→───┬─┬─┬─┬─┬──┬─┬───┬─┬─┐ 
│    │(│0│⍴│⊂│''│)│lex│,│⍵│
└───→┴→┴→┴→┴→┴─→┴→┴──→┴→┴→┘ 
┌→┐ 
│}│
└→┘ 

See also: words unify rmcm

Back to: contents

Back to: Dyalog APL

Trouble seeing APL font?