/media/sda-magnetic/david/Extern-Magnetic-2022-06-29/Extern01/Dokumente-2021-05-8/disk10-ab-2020-01-10/02-debian-pc2-work/informatik/java-new/2020-11-23/StreamProgLexer.java


public class StreamProgLexer {

    public static void main (String [] args) {
        try {
            int ch;
            CharEingabeStrom cs;
            cs = new StringLeser (args[0]);
            cs = new UmlautSzFilter (cs);
            //cs = new GrossBuchstabenFilter (cs);
            
            CharEingabeStrom while_stream = new StringLeserLoop ("WHILE");
            CharEingabeStrom begin_stream = new StringLeserLoop ("BEGIN");
            CharEingabeStrom end_stream = new StringLeserLoop ("END");
            CharEingabeStrom if_stream = new StringLeserLoop ("IF");
            CharEingabeStrom then_stream = new StringLeserLoop ("THEN");
            CharEingabeStrom else_stream = new StringLeserLoop ("ELSE");
            CharEingabeStrom do_stream = new StringLeserLoop ("DO");
            CharEingabeStrom empty_character_stream = new StringLeserLoop (" ");
            CharEingabeStrom newline_stream = new StringLeserLoop ("\n");
            CharEingabeStrom tab_stream = new StringLeserLoop ("\t");
            
            while ((ch = cs.read()) != -1) {
                if (empty_character_stream.read () == ch);
                else if (tab_stream.read() == ch);
                else if (newline_stream.read() == ch);
                else
                    System.out.println ((char) (ch));
            }
        }
        catch (ArrayIndexOutOfBoundsException e) {
            System.out.println ("usage: java StreamProg arg1");
        }
    }
    
    interface CharEingabeStrom {
        int read();
    }

    public static class StringLeser implements CharEingabeStrom {
        private String quelle;
        private int index;
        
        public StringLeser (String quelle) {
            this.quelle = quelle;
        }
    
        @Override
        public int read () {
            if ((index == quelle.length()))
                return -1;
            return quelle.charAt (index++);
        }
    }

    public static class StringLeserLoop implements CharEingabeStrom {
        private String quelle;
        private int index;
        
        public StringLeserLoop (String quelle) {
            this.quelle = quelle;
        }
    
        @Override
        public int read () {
            if (index == quelle.length()) {
                index = 0;
                return -1;
            }
            return quelle.charAt (index++);
        }
        
        public void reset () {
            index = 0;
        }
    }

    
    public static class GrossBuchstabenFilter implements CharEingabeStrom {
        private CharEingabeStrom quelle;
        
        public GrossBuchstabenFilter (CharEingabeStrom quelle) {
            this.quelle = quelle;
        }
        
        @Override
        public int read () {
            int zeichen = quelle.read();
            if (zeichen == -1)
                return -1;
            return Character.toUpperCase ((char) zeichen);
        }    
    }
    
    public static class UmlautSzFilter implements CharEingabeStrom {
        private CharEingabeStrom quelle;
        private int puffer = -1;
        
        public UmlautSzFilter (CharEingabeStrom quelle) {
            this.quelle = quelle;
        }
        
        @Override 
        public int read () {
            if (puffer != -1) {
                int zeichen = puffer;
                puffer = -1;
                return zeichen;
            }
            else {
                int zeichen = quelle.read ();
                if (zeichen == -1)
                    return -1;
                switch ((char) zeichen) {
                    case '\u00C4':
                        puffer = 'e';
                        return 'A';
                    case '\u00D6':
                        puffer = 'e';
                        return 'O';
                    case '\u00DC':
                        puffer = 'e';
                        return 'U';
                    case '\u00E4':
                        puffer = 'e';
                        return 'a';
                    case '\u00F6':
                        puffer = 'e';
                        return 'o';
                    case '\u00FC':
                        puffer = 'e';
                        return 'u';
                    case '\u00DF':
                        puffer = 's';
                        return 's';
                    default:
                        return zeichen;
                }
            }
        }
    }
}